Good Commits, History and Diffs
Write commit messages your team can use, keep commits focused, and inspect a project with git log, git show and git diff.
Welcome back!
Step 1 of 12Learn
A history that explains itself
Two developers inherit two projects. The first project's history reads: fix, stuff, more changes, asdf, WIP, final, final 2. The second reads: Add password reset email, Fix crash when cart is empty, Update tax rate for 2026, Remove unused payment provider.
A customer reports that checkout broke sometime last month. In the second project, the developer scans the log and finds the likely commit in a minute. In the first, they open commit after commit, reading code to guess what each one did.
Commit messages are notes to your future self and your team. They cost seconds to write and save hours later. A good message answers what changed in the subject line and, when it isn't obvious, why in the body.
The content of commits matters as much as the messages. A good commit is atomic: it contains one logical change, complete and working. "Fix crash when cart is empty" shouldn't also reformat 40 unrelated files and rename a variable across the app. If a commit needs the word and in its subject, it's often two commits.
Step 2 of 12Practice · Spot the mistake
This is an extract of a project log. Click every commit message that would be unhelpful to a teammate reading the history.
Step 3 of 12Learn
A simple commit message convention
Most teams follow a few conventions drawn from Git's own project:
- Subject line of about 50 characters or fewer. Long subjects get cut off in tools like GitHub and
git log --oneline. - Imperative mood, as if giving a command: "Add search box", not "Added search box" or "Adds search box." A handy test: the subject should complete the sentence If applied, this commit will...
- Capitalize the first word and don't end with a period.
- Blank line, then a body when the change needs explaining. Wrap the body at about 72 characters and explain why the change was needed and anything surprising about how.
For example:
Fix crash when cart is empty
The checkout page divided by the item count to show an
average price. With zero items this threw an error.
Show a friendly empty-cart message instead.Some teams add a prefix such as feat:, fix: or docs:, a convention called Conventional Commits that lets tools generate changelogs automatically. Whatever your team picks, consistency matters more than the specific rules.
Step 4 of 12Diagram
Anatomy of a good commit message
Tap a shape with to see more.
- Commit message
- Subject: about 50 chars
Short enough for
git log --onelineand GitHub lists.- Imperative mood
Write "Add search box", as if completing "This commit will ...".
- Capital, no period
A small convention that keeps logs tidy.
- Blank line
Separates the subject from the body so tools can tell them apart.
- Body: why, not how
Explain the reason and side effects; the diff already shows the how.
Step 5 of 12Practice · Code challenge
Some teams check commit messages automatically. Write isGoodSubject(msg) that returns true when the first line of msg is between 1 and 50 characters long and does not end with a period, and false otherwise. The message may contain more lines after a newline (\n).
isGoodSubject("Add search box to header")isGoodSubject("Fix typo.")isGoodSubject("")isGoodSubject("Fix crash when cart is empty\n\nThe checkout page divided by zero.")isGoodSubject("Refactor the entire payment module and also update the docs")
Step 6 of 12Learn
Reading history: git log and git show
git log lists commits, newest first, with full hash, author, date and message. It opens in a pager: use the arrow keys or Space to scroll and q to quit.
Useful variations:
git log --onelineshows one line per commit: short hash and subject.git log -5shows only the last five commits.git log --oneline --graph --alldraws branches and merges as a text graph, across all branches.git log --author="Lena"filters by author.git log -- chili.mdshows only commits that touched that file.git log -S "tax_rate"finds commits that added or removed that text, great for asking when did this line appear?
To see what a single commit changed, pass its hash to git show:
git show 3f9a1c2
You'll see the message followed by the diff of every file it touched. You only need enough of the hash to be unique in the repository; seven characters is usually plenty.
Two more references are handy. HEAD means the commit you currently have checked out, and HEAD~1 means the one before it, so git show HEAD~1 shows the previous commit without looking up its hash. git blame file.md shows, line by line, which commit last changed each line.
Step 7 of 12Practice · Match the pairs
Match each command to the question it answers.
Tap an answer, then tap the place where it belongs (or drag it there).
- git log --oneline
- git show a1b2c3d
- git log -- index.html
- git blame index.html
- git log --graph --all
Step 8 of 12Learn
git diff: what changed, line by line
git diff compares versions and shows the differences. Which versions depends on how you call it:
git diffshows changes in your working directory that are not yet staged.git diff --stagedshows changes that are staged and will go into the next commit. Run it before every commit as a final check.git diff main featurecompares two branches;git diff a1b2c3d HEADcompares a commit with the current one.
A diff looks like this:
@@ -3,4 +3,4 @@
Ingredients:
-2 cups flour
+2 1/2 cups flour
1 tsp baking powderLines starting with - were removed, lines starting with + were added, and lines with a leading space are unchanged context shown to help you find your place. A changed line appears as a removal followed by an addition. The @@ header gives line numbers: this section starts at line 3 in both the old and new versions.
A common surprise: you stage everything, run git diff, and see nothing. That's correct: plain git diff only shows unstaged changes, and you have none. Use --staged to see what you're about to commit.
Step 9 of 12Compare
Which diff shows what?
| Aspect | git diff | git diff --staged | git diff HEAD |
|---|---|---|---|
| Compares | Working dir vs. staging | Staging vs. last commit | Working dir vs. last commit |
| Answers | What have I not staged yet? | What will my next commit contain? | Everything I changed since the last commit |
| Empty right after git add . | Yes | No | No |
Step 10 of 12Try the calculator
How short can a commit hash be?
Each hexadecimal character has 16 possible values, so n characters can form 16n different IDs. Git shows enough characters to be unique in your repository, usually 7 or more.
- Possible distinct IDs (16n)
- 268,435,456
- Possible IDs per commit in the repo
- 26,844
Step 11 of 12Practice · Choose one
You staged all your changes with git add . and then ran git diff. It printed nothing. What does that mean?
Step 12 of 12Practice · Choose one
Which is the best commit subject line under the common convention?
Lesson complete!