GIT - 5.2 comparison of differences

This lesson focuses on comparing differences in Git with the diff family of commands. The starting point is a clean repository on master. We use our previously defined alias git hist to display the commit history at a glance.

Comparing two commits with git diff

To see the differences between two specific commit points, we use git diff with two commit references. We can also use the special HEAD pointer, which always points to the latest commit on the current branch. For example, to diff HEAD against an older commit:

git diff <older-commit-hash> HEAD

Git prints a textual diff for every file that changed between those two commits. The same command exists in a GUI variant, git difftool, which launches an external tool configured in your Git settings (for example P4Merge). Since multiple files can be involved, you cycle through them with the keyboard shortcut Ctrl+W after closing each one, until all files in the diff have been reviewed.

Diffing the working directory against HEAD

Once back in the terminal, we modify coucou.html in the editor, save the change and close it. Running git status confirms the file is modified, but it does not tell us what changed. To see the actual edits, we run git diff with no arguments:

git diff

This compares whatever has just been edited in the working directory against the repository state — that is, the last commit on the current branch. Git prints the added and removed lines for our small modification. We can also launch the same comparison in the configured GUI tool with git difftool, and exit the windows with Ctrl+W as before.

git diff is extremely powerful. Run git help diff to see all options — anything that can be passed to git diff can usually also be passed to git difftool. Knowing how to read a diff is one of the most important Git skills.