I’ve previously written about how to get the diffs between your working tree, the index and the last commit. But you frequently need to see the difference between the code you’re working on and previous commits.
Here’s a diagram that summarises the commands:
If you’ve read the previous article then this should make sense I’ve used HEAD~ to represent the parent of the tip of the branch you’re working on, but this can be replaced with the any of the other methods of referring to commits specified in the gitrevisions documentation.
However, when doing this, you usually only want to see changes between specific files. For this you can optionally pass in the paths that you want the diff between after a pair of dashes --. If you leave out the paths you’ll get the complete diff.
git diff --cached HEAD~ -- somefile.py
This will show the difference between the version of somefile.py in the index and the version in HEAD~
git diff HEAD~ -- somefile.py
This will show the difference between the version of somefile.py in the working tree and the version in HEAD~.
If you are using an external viewer such as Changes or Kaleidoscope (for the Mac) or other tools for other platforms. These usually come with instructions for setting them up and calling them using the git difftool command rather than git diff. The good news is that the options and parameters are the same so you can still get pretty diffs with the same control.