Let's Cover these Essentials: Git and GitHub

Let's Cover these Essentials: Git and GitHub

·

6 min read

Hello my friends, I think that it's pretty important to discuss the topics of what Git and GitHub are, as well as how and why we use these tools.

Like the other articles, this one will probably continuously be refined over the next few years as my understanding becomes more advanced.


Contents


What are Git and GitHub?

It's important to emphasize that Git and GitHub are two completely different things.

In simple terms, I like to think of Git as a local tool and GitHub as an online forum in which we can use the Git tool to create, read, and modify data.

Explaining Git

Git is a distributed version-control system and its purpose is to basically track the changes made in source code by taking snapshots of the modifications.

Git is regarded as an essential tool because we can use it to "go back" to old snapshots if we want to reverse any modifications to our files.

A significant feature of Git is branching. In a Git repository, there is a main branch in which all of our work is stored, but we can (and should) create more branches if we want to take the work from the main branch and expand it without messing around with the contents in the main branch.

Explaining GitHub

GitHub is identical to a forum, as people typically post content that they want to share with others online.

However, GitHub is special because it utilizes Git in order to allow for people to take that content and modify it on their own local computer, which is the whole reason why Git and GitHub are essential to understand as a developer who is expected to collaborate with other people on project(s).

With the use of GitHub and Git, it is easy for teams to collaborate on projects dynamically while ensuring that the files and data is secured.


Command Line (CL)

Before I get into using the Command Line for Git/GitHub, we should go over some common commands that we may be using time-to-time just to operate around the computer.

  • cd allows us to "change directory", or open files
  • dir or ls lists files
  • cls or clear clears the screen in Command Line
  • code opens VS Code/text editor of choice

Now that we have an understanding of Git, GitHub, and the Command Line, it's time to discuss the Git commands that will serve us well for many years.

Git Configurations

Since we're using Git to communicate to others, Git wants to know some of our information to identify who is doing what. It's also nice to have your Command Line configured with effective tools such as Visual Studio Code.

  • git config --global user.name "devduck"
  • git config --global user.email "devduck@example.com"
  • git config --global core.editor "code --wait" tells VSC to close when we exit
  • git config --global -e opens settings for configuration (run to check settings)
  • git config --global core.autocrlf true (if Mac, put input instead of true)
  • git config --global diff.tool vscode
  • git config --global difftool.vscode.cmd "code --wait --diff $LOCAL $REMOTE"

At the end of configuring Git, run git config --global -e and confirm these settings

[diff]
    tool = vscode
[difftool "vscode"]
    cmd = "code --wait --diff $LOCAL $REMOTE"

Git Functions

  • git clone ________
  • git init
  • git add ________
  • git commit -m "________"
  • git push origin ________
  • git status
  • git branch
  • git checkout -b ________
  • git checkout ________
  • git branch -d ________
  • git pull
  • git merge ________
  • git difftool (run before git add ________)
  • git remote add origin ________ (git clone automatically adds repo)
  • git remote -v
  • git rm --cached ________
  • git restore ________

In my opinion, I believe that these Git commands are the commands that we will need most. Most of these functions will be explained below, but just in case...

Click here for a great guide on Git Functions


Applying Git and GitHub

Here are some typical scenarios for using GitHub:

  1. You want to push your project onto GitHub to display it, or so others can work on it
  2. You want to pull another person's project from GitHub onto your own computer to work on it for fun, for your job, or for educational purposes.

As for using Git, you always use Git because it will keep track of your modifications, so you won't have to worry about screwing up a working version of your project and not being able to revert back.

Scenario 1 - Pushing to GitHub

Assuming you've configured your Git and GitHub through the Command Line, we need to first initialize a Git repository in order to store our files that we want to be tracked.

  • open the Command Line and cd ________ into the folder location
  • initialize the Git repository for your project with git init
  • stage the files that you want to be tracked with git add ________
  • check to confirm that these files are correct with git status
  • after you've confirmed, run git commit -m "________", leaving a note of what modifications you've made

Now that we've committed to Git, it's almost time to push your project to GitHub. However, we haven't initialized a remote repository to push to GitHub yet. Go to your GitHub and create a new repository. You can add the README file later to avoid any issues. Click the "Code" button to copy/paste the HTTPS link in the next step.

  • add remote repository "origin" with git remote add origin ________ and paste the HTTPS link
  • to check what remote repositories are linked, run git remote -v
  • we then run git push --set-upstream origin master to be able to push to the "master" branch in the "origin" repo

NOTE...

  • "origin" is equivalent to "name of repository" and we must push the "master" branch to our repository because Git specifically wants it to be called "master", but we can change the default branch name later.
  • Since the repo has been remotely set up, we can simply run git push to push.

Scenario 2 - Pulling from GitHub

To take an open-source project from the internet and put it on your local computer is very cool and convenient, thanks to Git and GitHub.

  • cd ________ into the directory that you want the project folder downloaded
  • on the project's GitHub repository, click "Code" and copy/paste the HTTPS link when running git clone ________
  • git pull when you want to obtain the latest updates on the project