Skip to content

Cheatsheet: Commit hygiene

Git stores snapshots. Every other command is just navigating those snapshots.

RuleGoodBad
Under 72 charsAdd login form validation(200-char rambling)
Imperative moodFix crash on empty addressFixed crash on empty address
CapitalizedAdd export featureadd export feature
No trailing periodUpdate READMEUpdate README.
WHAT, not WHYRefactor address-validatorMade the code better
Subject line (under 72 chars, imperative, capitalized, no period)
<- blank line
First paragraph: WHY this change exists. What problem
it solves. What the reviewer needs to know.
Second paragraph (optional): rejected alternatives and
the reasoning. Helps future readers understand why this
approach was chosen.
Closes #1234.
Terminal window
# Working directory has 3 unrelated changes (files A, B, C)
git status # see all 3 changes
git add fileA.js # stage only A
git commit -m "Fix crash on empty address"
git add fileB.js # stage only B
git commit -m "Refactor address-validator"
git add fileC.js # stage only C
git commit -m "Add CSV export to reports"
git status # confirm clean tree
git log --oneline # see 3 atomic commits
feat(scope): new feature feat(auth): add OAuth login
fix(scope): bug fix fix(api): handle null response
docs: documentation docs: update install guide
refactor(scope): restructure refactor(checkout): extract validator
test(scope): test changes test(auth): add password edge cases
chore: maintenance chore: bump deps
perf(scope): performance perf(query): index user_id column
style: formatting style: fix indentation

Type indicates change kind. Scope (optional) indicates code area. Description is the imperative-mood subject.

Anti-patternExampleFix
Vague verbfixSpecify what was fixed: Fix crash on empty shipping address
Kitchen sinkAdd login and fix nav and update docsSplit into 3 commits
End-of-day megacommitDay's workCommit at logical boundaries during the day
Fix-cascadefix, fix again, actually fixTest locally before committing; clean up with rebase if not pushed
Body-less complexRefactor user-service (200-line diff, no body)Add a body explaining the refactor’s motivation + design

L4 covers undoing things: git reset (move pointers, modify staging or working dir), git revert (create a new commit that undoes a prior one), git restore (discard working-dir or unstage changes), and git reflog (the safety net that lets you recover almost anything). The skills land directly on top of L3’s hygiene: knowing how to write a clean commit + how to undo a messy one closes the foundational loop.