Afraid of Git? Do a tutorial!

Ryan Tan
The Startup
Published in
7 min readMay 26, 2020

With thanks to Kenji Kamimura for taking the time to read and give me feedback on how to improve this article!

Background

When I participated in my first hackathon, we tried to build a website that allowed students to get help debugging their code. As my team had no knowledge of Git, we would all work individually on our own computers, developing each feature and then use a USB stick to pass our code around to one computer that our team calls the “master computer”. As you can see, this is extremely slow, and we soon figured out that this was a stupid way of doing things. Our organizers felt bad for us and decided to teach us Git.

It’s extremely hard to work on a single computer together. Artist: David Ng. Facebook. Instagram.

After the Hackathon, I wanted to utilize Git, so I went and read guides and practised wherever I could. I noticed that when I was learning how to use Git, most guides do not have visuals. My take on this Git guide is to include visuals where possible, to help newcomers learn better and faster (and hopefully with a less steep curve).

Get to the point: Learning how to use Git by doing!

To get started, we obviously need Git on our computer. If you do not have Git installed, you will have to download and install Git here: https://git-scm.com/. Once installed, we can now open and use Git! So go ahead and open up Git (a.k.a. Git Bash in Windows, git in terminal for MacOS).

To practice, let’s now create a folder, and name it git_practice (or whatever you’d like to name it). This folder will serve as our “HQ” for the files that we would like to track. For demonstration purposes, in this folder, I’d like to track git_test.py. You can choose to create a new file, or copy over some old files that you would like to track.

(Left) Added some code to a brand new Python file. (Right) What my folder current consists.

Once you’ve copied over the files that you’d like to keep track, head over to www.github.com and create a new repository (and create an account if you don’t already have one). I’ve set the settings shown below, so feel free to do the same:

So now we’re ready to track our files. In the Git terminal, navigate to the directory that you have just created, using cd. For example, I created my folder in my Desktop, so I’d have to type in the following lines to get to my directory:

cd desktop
cd git_practice
Navigating to the directory that consists of files that we’d like to track.

Once you’ve arrived in the directory, type in:

git init 

The above command initializes a .git subdirectory in the current working directory, which will contain all of the necessary Git metadata for the new repository. If you’re not sure what this means, click on this link to learn more!

Initialized an empty, local Git repository.

Now we have to add a new remote to this repository. A remote in Git is basically a common repository that all group members use to exchange their changes. In this case, our remote repository will be stored on a code-sharing service: Github.

Navigate to the Github page that you’ve just created, and click on the “Clone or download” button shown below, and copy the web URL.

The clipboard helps copy the URL!

In the Git command line, type:

git remote add origin {<copied URL here>}
This command creates a new remote called origin, which is located at the web URL.

Great! We’ve just added our remote origin. To check whether this has been done successfully, type in:

git remote -v
Checking to see if the remote has been added.

Now we would like to see the status of files that we’d like to keep track of. To do this, type in the following command:

git status
In the case, git status is showing files that have not been tracked.

We now want to add a change to the staging area. Once these changes are added, we can check to see if they have been tracked by doing the following:

git add {<file_name>} // This command adds files to be tracked.
git status
We have successfully tracked the python file!

Now we’re ready to commit! Important: Always commit with meaningful descriptions. If you don’t, your future self will get annoyed at your current self. So do yourself a favour and add meaningful descriptions! Only takes a couple of minutes at most.

Let’s now add a meaningful commit message! You can choose to do EITHER the first line or the second, NOT BOTH:

git commit -m "Initial Commit"
git commit -m "{<your message here>}"
We have now added a commit message!

Yay! Almost there! Now, all we have to do is to push our file onto the Github repository!

git push origin master
The above error message is intimidating to new users.

Oh no! Our push got rejected. Let’s analyze what the issue is. Important: If you were to google this error, someone somewhere would suggest you do:

git push -f origin master

Do not do this! This is extremely dangerous and should not be used unless specified by your manager, or you know what you’re doing.

It seems like the issue lies in work that we do not have locally. What this usually means: somebody has made changes to the repository that was not tracked locally in your current working directory.

In our specific case, if you look into the repository on Github, you’ll see a README.md. Now, this file initially wasn’t present in our local working directory, therefore Git doesn’t know if your current working directory is the same as the specified remote repository.

Let’s attempt to solve this. We first want to fetch all the files by downloading them from the remote and then merge these files with the current working directory. To do this, we type:

git pull origin master

This command should fetch (get all the files downloaded from the remote) and merge (merging the current working directory and the pulled files). We can then see the following photo:

More information about our error!

We now have more information about the error. What this now means is that: we have just created a new repository and added a commit to it (in our case the README.md — with the commit name “Initial Commit!”), and now we’re trying to PULL from a remote repository that already has commits of its own. Git will throw an error in this case, since it doesn’t know that both these projects are somehow related.

We can resolve this by allowing unrelated histories. To do this, type in the following:

git pull origin master --allow-unrelated-histories
Our files have been merged using this line of code!

p.s., an editor might pop up asking to write a commit message. In this tutorial, we just want to exit, so press the “Esc” button, followed by the words “:wq” to exit.

We have now solved the issue! If we were to push our code now, it should all hopefully work!

Success! Congratulations on your first push to Github!

And… It has!

Congratulations on pushing your first commit! Now you can make further commits to this repository, by building more on top of your initial code!

I hope this guide helps you understand Git a little better. In this tutorial, we went through:

  • Creating a new repository, on Github and on a local working directory on your computer.
  • We encountered an error.
  • We analyzed what the error was about.
  • We resolved the error.
  • We have pushed our code to Github.

We will cover pull requests (the heart of collaboration on Github) in another tutorial, which will be linked here (soon!). I hope this tutorial has helped make git a less frightening and more enjoyable experience for you!

--

--

Ryan Tan
The Startup

Final year Computer Science student that loves sharing knowledge and personal experiences!