This article kicks off the first in a series on how to use Git & GitHub. Understanding how to use Git is essential to any developer, and GitHub makes it easy and free to store your code in version control, a fancy term meaning you are taking snapshots of your code at certain points in time. This article assumes you have git installed on your computer already, but if not, click here to install it. Lets dive in!

Creating a Repository

Repositories are like storage containers for your code. They separate your project from everyone else's on GitHub. To create a repository, log into GitHub and click the + button in the upper right and then New Repository.

https://cdn.brianmorrison.me/media/2020/80372ab2-4fb9-4da9-9ffa-57f45b03ca85

In the Create a new repository view, give your repository a name, all other fields are optional and are relatively self explanatory. One thing I wanted to call out is the options under Initialize this repository with: section. If any of these options are checked, you'll need to pull the repository before you are able to sync existing code with it. I personally never use these options, especially if I am looking to push an existing local repository up to GitHub as it could cause issues.

https://cdn.brianmorrison.me/media/2020/dd461ec8-3380-42b5-8a3d-e73bfaf090a0

Syncing your Code to GitHub

There are three methods worth calling out when it comes to syncing code with GitHub; Cloning, Pulling, and Pushing.

Cloning

Cloning is what you'd do when you want to get an initial sync of code from GitHub to your workstation. It will pull down a copy of whatever code is currently in the repository. Using the example of the repository above, you should see a screen like this when the repository is finished. Note the URL that is used to clone the repo.

To clone the repository, open up the terminal on your computer and navigate to a folder where you want the code to be stored. The following command will do two things. Firstly, it will create a folder INSIDE your current folder that is the same name as the repository. Second, it will copy all of the code from GitHub into that folder it created.

git clone https://github.com/bmorrisondev/my-test-repository.git

If this is the first time you are cloning from GitHub on your computer, your terminal will prompt you for your GitHub credentials. Otherwise it will use the account you previously signed in as.

You can also specify the folder if you want to by appending it to the end of the command. Git will then create the new folder with the name you specified. If I were to execute the following command in C:\Repos, all of my code will end up in C:\Repos\my-awesome-repo.

git clone https://github.com/bmorrisondev/my-test-repository.git my-awesome-repo

Pushing

Pushing is the process of pushing your list of new commits up to GitHub. Note that it is the commits that will be pushed, so you'll need to make sure to commit any code into your local repository before it will be pushed to GitHub.

Lets add a file and commit it. I'm going to add a single file called My New File.txt. Then when I use git status, we can see the file is currently untracked.

https://cdn.brianmorrison.me/media/2020/8a4fa1b5-f04a-429b-bc1a-576cf482b8bf

https://cdn.brianmorrison.me/media/2020/56b81444-e500-4a20-89d6-f3ac450b0593

Lets commit the file with git add . followed by git commit -m "Your commit message".

https://cdn.brianmorrison.me/media/2020/a926c23f-b6f5-4d7c-afce-07433f680460

Once the file is committed, we can push it up to GitHub. Git uses what's called a remote to inform it where it should be pushed to. You can see a list of remotes on your repository with git remote -v. If you've cloned a repo from GitHub, this will be setup automatically. We can issue the following command to push our changes to GitHub.

git push

https://cdn.brianmorrison.me/media/2020/17c35fa8-44fa-46ed-bd99-cdbf3802bcdf

Now if you are pushing existing code that was not cloned down, you'll need to set up the remote before you can push. To do this, have your terminal open in the same directory as your repository and issue the following commands, replace the repository URL with whatever your git URL is in GitHub.

# This command registers a remote called 'origin' with the git URL
git remote add origin https://github.com/username/reponame

# This command will push your code to the remote origin with the branch named 'branch_name'. Branch name is usually 'master' or 'main'.
# This will also configure your local repo to 'track' its current branch with 'branch_name'
git push --set-upstream origin branch_name

https://cdn.brianmorrison.me/media/2020/8a07b2ce-9c86-406c-896f-73418c2c88d0

Pulling

Pulling is what you'll do when others (or you on another workstation) have pushed commits up to GitHub and you want to pull those new commits down into your local machine. Provided your remote is setup as discussed in the previous section, you can do this simply by issuing the command;

git pull

In this example, I made a change to the text file in my repository directly in GitHub, which is simply used to simulate changes being made somewhere other than your local workstation.

https://cdn.brianmorrison.me/media/2020/2b43bacb-98b5-4e0d-ae1d-222e8be40335

Now if I compare the commits in GitHub with the commits locally (using git log), you'll see there is one new commit in my GitHub repository that is not in my local repo.

https://cdn.brianmorrison.me/media/2020/bd21e9f8-5af9-4a68-b510-ec0a44ef59a4

Issuing git pull will bring those changes in as well as update the text file on my computer.

https://cdn.brianmorrison.me/media/2020/dca004b1-7783-4487-996c-a7c728f88f11

In the next article, I'll cover branching and how it works.