How Much Belongs in One Commit
Topic 14

How Much Belongs in One Commit

History

Nobody tells beginners how big a commit should be, so everyone works it out by overshooting in both directions: one commit a week for a while, then a commit per saved file after someone says commits should be small.

The useful rule is not about size. One commit is one complete idea — something you can describe in a single sentence without the word "and".

Too big

A whole day's work in one commit is the more common mistake, and it costs you three things at once.

You cannot undo one part of it. Chapter 4 can remove a commit cleanly, but if that commit contains a good change and a bad one, removing it takes both. You cannot review it, which matters the moment somebody else is reading your work in Chapter 8. And you cannot describe it honestly — which is why the message ends up saying "update".

Too small

The opposite is a commit every time you save a file. The history becomes a list of keystrokes, and the three commits that actually mattered are buried among forty that did not.

The reader you are writing for is a person scanning git log --oneline and trying to see the shape of the work. Forty entries called "wip" tell them nothing.

The sentence test

Before committing, write the message in your head. If it needs the word "and", you are probably holding two commits.

This is where staging pays for itself. Say you added the sightings list and, while you were there, fixed the header spacing. Two ideas, two messages, two commits:

Split one session into two commits
git add sightings.js
git commit -m "Add the recent sightings list"
git add style.css
git commit -m "Tighten the spacing under the header"

Between those two commands, git status shows style.css still sitting in the working directory, exactly as you left it. Nothing was rushed and nothing was undone.

The same afternoon, committed three ways
One giant commit
"update site" — undoable only as a whole, reviewable by nobody
Eleven tiny commits
"wip" × 11 — safe, unreadable, and hiding the three that matter
Three real commits
sightings list · header spacing · meeting time — each one separately undoable

Commit when it works

One more guideline, and it is the one that turns the history into something you can actually travel back to: commit at a point where the project works.

For Sandpiper, "works" means the page opens and looks roughly right. A commit that does not work is a trap for whoever visits it later — including you, hunting for the change that broke something, and finding a state that was broken for an unrelated reason.

This does not mean waiting until a feature is finished. A feature can take a week, and a week without commits is a week with no safety net. It means finding the small steps that each leave the project in one piece.

A rhythm

Several commits an hour is normal while the work is going well. If two hours have passed with nothing committed, that is usually a signal rather than a virtue — you have probably started three things at once and none of them is finished.

A practical response: stop, run git status, and see whether one of the three is complete enough to commit on its own. Almost always one is, and the other two get easier the moment it is out of the way.

Common Confusions
  • "More commits is always better." More coherent commits is better. Ten commits called "wip" are worse than one honest one, because the reader has to open all ten.
  • "I should only commit finished features." Commit each working step of the feature. Otherwise a week of work sits unrecorded, which is a week where the whole of Chapter 4 cannot help you.
  • "I'll tidy the history up later." Tidying history means rewriting it, which this book does not teach and which is genuinely risky on shared work. Commit well the first time; it is cheaper.
  • "A commit has to change only one file." One idea often spans several files — a new page and its stylesheet belong together. The unit is the idea, not the file count.
Why It Matters
  • Commit size decides whether git revert in the next chapter can undo one mistake, or is forced to undo a good change that happened to travel with it.
  • Reviewers on a team read commit by commit. A coherent series is the difference between a ten-minute review and an hour of untangling.
  • Committing at working points makes the history a place you can safely travel back to, which is what makes Chapter 4's recovery commands worth having.

Knowledge Check

What is the test for whether you are holding one commit or two?

  • Whether you can describe the whole thing in one sentence without "and"
  • Whether the changes are confined to a single file in the project
  • Whether the total number of changed lines stays below about fifty
  • Whether all of the changes were made within the same hour of work

Why does an oversized commit hurt when you later need to undo something?

  • Undoing it removes the good changes along with the one that was wrong
  • Git refuses to undo commits that change more than one file at a time
  • Large commits are stored differently and cannot be reversed automatically
  • Undoing it requires rewriting every commit that came after it in the history

You have edited two files for two unrelated reasons. What does staging let you do?

  • Commit one file now and leave the other untouched for its own commit
  • Commit both files but attach two separate messages to the single commit
  • Commit the first file and automatically discard the changes in the second
  • Nothing yet, because both files must be committed together once they are edited

What does "commit when it works" mean in practice for a week-long feature?

  • Commit each small step of the work that leaves the project in one piece
  • Wait until the entire feature is finished and tested before committing anything
  • Commit constantly and fix any broken states later by rewriting the history
  • Only commit at the end of each working day, whatever state the project is in

You got correct