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.
Summary
This lesson demonstrates how to compare differences between Git commits and the working directory using the `git diff` and `git difftool` commands. You'll learn to view changes between specific commits, use visual diff tools for better clarity, and navigate multiple modified files. The tutorial also covers examining modifications between your working directory and the Git repository index.
Key points
- Use `git diff` to compare changes between two commits or between the working directory and the repository
- Apply `git difftool` to open a configured visual diff tool (like VS Code) for easier comparison of multiple files
- Navigate through multiple modified files using `Ctrl+W` to switch between diff windows in the visual tool
- Understand the difference between changes in your working directory versus changes already staged in the repository
- Combine `git diff` with file paths to focus comparisons on specific files
- Consult `git help diff` to explore additional options and powerful filtering capabilities
FAQ
What is the difference between `git diff` and `git difftool`?
`git diff` displays text-based differences directly in the terminal, while `git difftool` opens a configured visual tool (such as VS Code or a dedicated diff viewer) that makes it easier to navigate and understand changes, especially when multiple files are affected.
How can I compare changes between my working directory and the staged area?
Run `git diff` to see unstaged changes in your working directory compared to the index (staged area). Use `git diff --staged` or `git diff --cached` to see changes that have already been staged but not yet committed.
Can I use `git diff` to compare specific commits?
Yes, use the syntax `git diff <commit-hash-1> <commit-hash-2>` to compare two specific commits, or reference commits by their position relative to HEAD using pointers like `HEAD~2` to view historical differences.