Intro to Git-1: Basics

If you are unfamiliar with Git and the process of version control, this will be a tutorial to teach the basics so you can easily implement version control in all of your projects.

Why use Git?

It is important to understand the purpose and usefulness of a version control system such as git. One of the reasons it is so widely used is it allows people to feel comfortable making mistakes.

It is natural for a programmer to make a mistake and accidentally severely break the code. If a bug is noticed and it is not very severe, you might be able to CTRL+Z your way back to safety. But mistakes are not always so small. What if you added a new feature to your program and decided that you don’t want it anymore? It would be too much work to try to find all the code you wrote regarding that feature and manually delete it.

How Git Works

Git allows you to store commits (snapshots of your code at a certain point of time) so that you can always go back to the state your code was at during that commit. I believe that the best and “easiest” way to use git is via command line. The word “easiest” is quoted because command line git requires a little bit more of an understanding of what is going on than a visual version control software would, but thats a good thing!

Commits

Lets say your starting a new project with a file myProgram.java. You have added some simple code to this file and it is in a stable condition. Then you decide you want to add the next feature to this file. However, whenever adding new features to code, there is a chance that you might mess up the project. So we create a commit or snapshot of our code when it is stable.

The git command “git status” will give you this:

Screen Shot 2016-09-15 at 8.34.01 PM.png

Under “untracked files” you can see all the files that you have either created or edited are in red. “myProgram.java” is there since I just created it. Lets create a commit to save this version of my file.

Creating a commit

There are 2 steps to create a commit.

  1. Add the files to be committed
  2. Commit the files added

To add the file you simple type “git add <file name>”. In this case our file name is “myProgram.java”.

Screen Shot 2016-09-15 at 8.37.46 PM.png

You can see that after adding the file, if you type “git status” again, it appears as green under “changes to be committed”.

Once you have added all the files you would like to commit, you can type:

“git commit -m “<commit message>””. The commit message parameter after “-m” is a small message to describe the commit. Be sure to put your message in quotes.

After you commit, you can type “git log” to see a list of previous commits. Right now I only have one and you can see the commit message I gave it.

Screen Shot 2016-09-15 at 8.40.58 PM.png

Commit History

Think of all your commits together as a timeline of your code. When typing “git log” it organizes the commits, showing the earliest ones first. Since I created a commit of my code, I can always go back it if things are not working out the way I would like.

Here I added a few more commits of code.

screen-shot-2016-09-15-at-8-50-35-pm

Every commit has more additions to the code and the convention is to only commit code when we are happy with the way things are working.

Inevitably, over time, code will break.

Lets say that I am working on version four of my program and things go wrong. Well thats not a problem since git will let us go back to a previous state of our code in one command!

If I want to reset the current state of my code all I need to do is grab the SHA (the weird string of characters and numbers given to each commit). You can find the SHA for each commit on top of the commits shown when typing “git log”.

For example, in order to go back to my commit with the message “second version”. The SHA is 3c6f41879b31dcbd57dfad333a26876678e4fa03. So all I need to do is copy this SHA and type “git reset –hard <SHA>” where <SHA> is the SHA for the commit I want to reset to. The “–hard” parameter tells git to disregard any of the files I changed or added. Which is what I want since the program is messed up.

Screen Shot 2016-09-15 at 8.58.13 PM.png

Now if I type “git log” I should see that i’m back at the old commit and my code was reverted to the state that it was at during that commit.

Screen Shot 2016-09-15 at 8.58.24 PM.png

That’s it!

Git is very easy to use version control system that can save you a lot of trouble when your stuck!

 

 

 

 

 

 

 

 

2 Comments

  1. […] This is part 2 of my Intro to Git Series. If you are unfamiliar with git basics I would recommend checking out part 1 of the series. […]

  2. […] Intro to Git-1: Basics […]

Comments are closed.