Git Crashcourse

git_feat

Tracking Changes

Git includes some functions to monitor changes made to individual files and the repository itself. Take for example if we were to add a new file to our “simplesite” project:

bash$ touch testfile
bash$ git status
# On branch master
# Untracked files:
#   (use "git add ..." to include in what will be committed)
#
#	testfile
nothing added to commit but untracked files present (use "git add" to track)

When we run “git status”, Git tells us that a new file named “testfile” has been added to the project, but that it is currently not being tracked as it wasn’t added to the repository. If we wish to track this file as part of the project we could run “git add testfile”, or else it will simply be ignored when the next commit is made.

You can also use the “git log” command to see when commits were made:

bash$ git log
commit e28f5f19cc17e71ff389acfa47782b6b80c178ca
Author: Tom Nardi 
Date:   Mon Jul 23 13:02:51 2012 -0400

    First commit for simplesite

With this information we can see when commits were made, and by whom. This display shows why it is so important to add in your name and email as mentioned earlier in the guide, as your commits will be signed with your identification information so others will know who made the changes.

Reverting Changes

So far, all we have done is save the changes we’ve made. What if we want to go back to how things used to be, as in the case of a mistake?

Let’s say we make a change to a file that we decide we aren’t quite happy with:

bash$ rm index.html
bash$ echo "<b>Go home we don't like you</b>" >> index.html

Well, that’s not very nice. Let’s see if we can’t fix that.

First, we get the hash from our previous commit, then restore the index.html file to the state it was in at that point:

bash$ git log | grep commit
commit e28f5f19cc17e71ff389acfa47782b6b80c178ca
bash$ git checkout e28f5f19cc17e71ff389acfa47782b6b80c178ca index.html
bash$ cat index.html
<b>Welcome to my site</b>

Our index.html file has now been restored to the state it was in when the commit was performed, reverting the changes which were made. You can also use the command “git reset” to reinitialize the entire project to an earlier commit:

bash$ git reset --hard e28f5f19cc17e71ff389acfa47782b6b80c178ca
HEAD is now at e28f5f1 First commit for simplesite

Not Just For Developers

At this point, you should have a pretty good idea of how changes are tracked and Git’s ability to bring you back to a previous point in time.

While Git was designed to maintain changes in the Linux kernel, you don’t need to be a developer to recognize how useful Git is. Whether your writing a novel, editing images, or producing audio, you could certainly stand to have a reliable method of reverting changes and tracking progress.

If your produce anything on your computer which you would like to keep safe, Git might be the solution you’re looking for.

Tip Of The Iceberg

As mentioned in the beginning of this article, this is meant as only the most basic of introductions to Git. I’ve walked you through the basics of initializing a repository and committing changes,  but Git is capable of so much more; such as maintaining different branches of changes and pushing changes to remote sites such as github.

If you’d like to look into some of the more advanced functions of Git, check out the official documentation; an excellent resource which walks you though essentially every function of Git. In addition, be sure to keep an eye out here on “The Powerbase” for future Git articles.

Tom Nardi

Tom is a Network Engineer with focus on GNU/Linux and open source software. He is a frequent submitter to "2600", and maintains a personal site of his projects and areas of research at: www.digifail.com .

Related posts

Top