While git diff
on its own shows changes in your working directory that haven't been staged, its real power lies in comparing different points in your project's history. By providing specific commit identifiers, you can see exactly what changed between any two snapshots. This is incredibly useful for understanding how a feature evolved, tracking down when a bug was introduced, or reviewing changes made by collaborators.
Before you can compare commits, you need a way to refer to them. Every commit in Git has a unique identifier called a SHA-1 hash (or simply "hash"). It's a long string of hexadecimal characters (like f7b1d2a4e9c3...
). You typically see these hashes when you use the git log
command.
$ git log --oneline
a8e3c4d (HEAD -> main) Add usage instructions to README
f7b1d2a Fix typo in introduction
9c3a1b0 Initial project setup
You don't usually need the entire hash. Git is smart enough to figure out which commit you mean as long as you provide enough characters to make it unique, often just the first 7 characters are sufficient (like f7b1d2a
).
Git also provides convenient ways to refer to commits relative to the current state (HEAD
represents the currently checked-out commit):
HEAD
: The most recent commit on the current branch.HEAD~1
or HEAD~
: The commit before HEAD
(its parent).HEAD~2
: The commit two steps before HEAD
(its grandparent), and so on.main
or feature-branch
) to refer to the latest commit on that branch.The most direct way to see the differences between two points in history is to provide both commit identifiers to git diff
:
git diff <commit1> <commit2>
This command shows the changes required to transform the state of the project at <commit1>
into the state at <commit2>
.
Let's use our example history:
A simple linear history with three commits.
To see all the changes introduced between the initial setup (9c3a1b0
) and the final commit (a8e3c4d
), you would run:
git diff 9c3a1b0 a8e3c4d
Or, using the short hashes:
git diff 9c3a1b0..a8e3c4d # The .. syntax is equivalent here
The output will show a combined diff representing all modifications, additions, and deletions made across the commits f7b1d2a
and a8e3c4d
compared to the starting point 9c3a1b0
.
Sometimes you want to compare an older commit with your current working directory (including any unstaged changes). You can do this by providing only one commit identifier:
git diff <commit>
For example, to see how your current files differ from the commit where the typo was fixed (f7b1d2a
):
git diff f7b1d2a
This is useful for seeing how far your current work has diverged from a specific historical point.
A very common task is to see what changed in the last commit. You can achieve this using the relative references:
git diff HEAD~1 HEAD
This command specifically compares the parent of the current commit (HEAD~1
) with the current commit (HEAD
), showing only the changes introduced by that most recent commit.
Remember, the output of git diff
uses a specific format:
-
indicate content removed from the first file/commit.+
indicate content added in the second file/commit.diff --git a/file.txt b/file.txt
indicate which files are being compared.--- a/file.txt
and +++ b/file.txt
specify the "before" and "after" files in the comparison.Being able to compare different commits is fundamental to understanding your project's history. It allows you to pinpoint changes, review specific development steps, and gain insights into how your codebase has evolved over time. Experiment with git diff
and different commit identifiers using git log
to become comfortable navigating your project's past.
© 2025 ApX Machine Learning