"What is mind? No matter. What is matter? Never mind." - Thomas Hewitt Key, 1799-1875

Introduction to using git locally

Wednesday 14th January 2009

Categories: Guides, Code, FLOSS

Looking back

To find out what's happened between commits, you can use git's diff tool. Just entering:

git diff

will let us look at the difference between the index and the working tree. If we want to see the differences between what we've got now in the working tree, and some other commit, simply enter:

git diff <my-commit>

So, to compare the working tree to the last commit, we enter the command:

git diff HEAD

If we want to use the index rather than the working directory, we can use the --cached command. For instance:

git diff --cached

will let us look at changes made between the last commit i.e. HEAD, and the index. If we specify a specific commit we can compare the index to that commit like so:

git diff --cached <my-commit>

If we want the difference between two specific commits, we just name them both:

git diff <commit-one> <commit-two>

This will show the changes made between <commit-one> and <commit-two>.

To view the various commits of the current branch, use:

git log

If you prefer a graphical view of what's going on, there are various GUIs you can use - qgit provides a GUI using the Qt toolkit.

You can also view the current status of the repository using:

git status

Fixing mistakes

Being human, we all make mistakes. Therefore, git has a few handy commands to help us get rid of them. First of all, let's say you've just made a commit, but forgot to add a file that should have been committed. All you have to do is update the index to whatever you want to be in the commit, and then issue the command

git commit --amend

Without the --amend flag, git would commit the index as a separate commit. With the flag, it simply updates the latest commit.