Comparing Two Commits
Git diff between commits.
Git diff isn't only for uncommitted changes. You can compare any two commits, to see everything that changed between them, across all files.

- diff works between commits
- Any two points in history
- All files at once
Diff between two hashes
Git diff, then two hashes. The older one first, then the newer one. Here we compare the first commit with the latest. Dash dash stat gives a short summary of which files changed, and how much.
git diff --stat f7d9757 HEAD about.html | 2 ++ index.html | 3 +++ style.css | 3 +++ 3 files changed, 8 insertions(+)
Using HEAD and tilde
Instead of hashes, you can count back from head. Head tilde two means two commits before head. So git diff head tilde two, head, shows everything from the last two commits. One line in the style file, and one on the home page.
git diff HEAD~2 HEAD diff --git a/index.html b/index.html index 93dd595..d4173c3 100644 --- a/index.html +++ b/index.html @@ -1,3 +1,4 @@ <h1>My Site</h1> <p>Welcome!</p> <a href="about.html">About</a> +<p>New: contact me anytime!</p> diff --git a/style.css b/style.css index 433e28e..a9b3d41 100644 --- a/style.css +++ b/style.css @@ -1,2 +1,3 @@ body { font-family: sans-serif; } h1 { color: navy; } +p { line-height: 1.6; }
Ways to name a commit
Here are the ways to point at a commit. A hash, or just its first seven characters. Head, the commit you're on. Head tilde one, its parent, and head tilde three, three commits back. And a branch name, which means the latest commit on that branch.
| key | value |
|---|---|
| f7d9757 | a short hash |
| HEAD | where you are |
| HEAD~1, HEAD~3 | 1 or 3 commits back |
| main | the tip of a branch |
Summary
Git diff, with two commits, compares any two points in history. Head tilde N counts back from where you are. Next, the best part. Undoing mistakes.
Key takeaways- git diff <old> <new>
- --stat for a summary
- HEAD~N counts back