Module lessons (3/5)
Atomic commits & writing meaningful history
Why One Commit = One Logical Idea
Anyone can sweep an entire afternoon into git add . && git commit -m "stuff". The bill arrives two weeks later when an urgent security fix is tangled inside a 40-file formatting commit and cannot be safely reverted without tearing down working features.
An atomic commit isolates a single coherent unit of change — every file in that snapshot serves one purpose, compiles cleanly, and carries an informative header.
| Practice | Commit Shape | git revert Impact | Code Review Experience |
|---|---|---|---|
Mega-commit (git add .) | Mixes unrelated bugfixes + features | Reverts everything at once | Painful context switching |
| Atomic commits | One logical change per commit | Clean 1-line surgical undo | Fast diff comprehension |
| Conventional prefixes | feat:, fix:, refactor:, docs: | Self-indexing release notes | Clear grep/log scanning |
Stage & Separate Changes Interactively
Walk through modifying multiple files, staging just one piece into the Index, committing it first, and reserving the rest inside the Four Areas simulator:
Example 1 — Splitting Two Edits in the Working Tree
Even when src/server.ts and src/config.ts sit modified in the same directory, stage and commit them as two crisp snapshots.
git add src/server.ts
git commit -m "fix: switch server port to 8080"
git add src/config.ts
git commit -m "feat: add dev environment config"Example 2 — Anatomy of a Conventional Commit Subject
Keep the subject line under 72 characters in imperative mood ("add rate limiter", not "added"), prefixed by change type:
git commit -m "feat: add JWT expiration check" # New capability
git commit -m "fix: guard against empty tax rate" # Bug repair
git commit -m "chore: configure .gitignore defaults" # Tooling/maintenanceExample 3 — Shortcut -am Only Touches Tracked Files
git commit -am "..." stages all already-tracked modifications and commits in one step — remember it skips untracked new files (??).
echo "export const port = 9000;" > src/server.ts
git commit -am "chore: update default dev port"
# New untracked files still need an explicit git add <path>!Example 4 — Auditing Commit Boundaries with git log --oneline
Verify after committing that every snapshot tells a coherent chronological story.
git log --oneline -n 3
# e4f2a10 feat: add dev environment config
# b9c1d02 fix: switch server port to 8080
# 7a11c33 chore: initial project setupStep 1 — Commit the server port change cleanly on its own
Stage ONLY src/server.ts and record an atomic commit for the port fix while leaving src/config.ts untracked in the Working Tree.
Show hint
Use git add src/server.ts followed by git commit -m "...". Do NOT run git add . yet.
Solution available after 3 attempts
Step 2 — Split the dirty workspace into two atomic commits
Replace the temptation to run git add . with two distinct atomic commits: commit src/server.ts first, then commit src/config.ts separately.
Show hint
Run git add src/server.ts -> git commit -m "..." -> git add src/config.ts -> git commit -m "...".
Solution available after 3 attempts
Step 3 — Build a two-commit feature stack from scratch
Initialize the repo, commit README.md and src/app.ts as commit #1, then create src/logger.ts containing 'export const log = true;' and commit it cleanly as a separate 'feat:' commit.
Show hint
Sequence: git init -> commit README.md + src/app.ts -> create & stage src/logger.ts -> git commit -m "feat: ..."
Solution available after 3 attempts
Why is git commit -m 'fix: prevent negative checkout discount' considered far healthier than bundling 14 bugfixes into git commit -m 'misc fixes'?
You edit src/server.ts (already tracked) and create a brand-new file src/health.ts (untracked), then run git commit -am 'feat: add health endpoint'. What ends up in that new commit?
echo "export const health = true;" > src/health.ts
echo "// updated" >> src/server.ts
git commit -am "feat: add health endpoint"