The States of Git

One of the things I like about git, is the concept of working directory, index and repository. The index, or staging area, is really useful for gathering related changes before committing to the repostitory.

The GitGuys have a good description of the difference between these three.

Sometimes we will have changes in the working directory and/or the index. To compare differences between these different states:

Command Useful Alias Working
Directory
Index Repo (HEAD)
git diff gd
git diff --cached gdc
git diff HEAD gdh

If you want to diff a particular file, add -- filename to end of command e.g.

  git diff -- src/sql/update_20140125a.sql
  git diff --cached -- src/sql/update_20140125a.sql
              

To see contents of a file in a particular state

State Command
Working Directory cat filename
Index git show :filename
Repo (HEAD) git show HEAD:filename

And to get a good understanding of how git works, take a look at Git from the inside out