Tutorials

Commit single lines: precise commits with line staging

How to stage only parts of a change and build self contained commits that are easier to review and to undo.

Commit single lines: precise commits with line staging

Why whole files are often too coarse

While working, several independent changes quickly pile up in a single file. A fixed bug, a small rename, a quick debug print you do not actually want to commit. If you can only stage whole files, all of that lands in the same commit. The history becomes muddled, and a later rollback always hits several things at once.

Good commits, by contrast, tell a clear story. Each commit does exactly one thing. That is precisely what staging individual lines is for.

What line staging means

Line staging, also called partial staging, means you do not stage the whole file but only selected lines or blocks of a change. That way you deliberately build a commit that does exactly one thing and leave the rest in the working directory until its turn comes.

Technically Git splits a change into blocks (hunks). Line staging goes finer still and lets you pick individual lines within a block.

Why self contained commits are so valuable

A commit that does only one thing is easier to read, easier to review and easier to undo. If it later turns out that one change was a problem, you can revert exactly that one commit without dragging along unrelated changes. It pays off when cherry-picking onto another branch too, because you transfer precisely that one change.

Staging lines with Spire

In Spire you select the lines that should go into the next commit right in the diff, through the selection in the gutter. You can tap individual lines or drag a range within a block using the shift key. Only what is selected gets staged, the rest stays untouched. Conversely, individual lines can just as precisely be taken out of the selection again or discarded. Because Spire runs natively on the Mac, the selection stays smooth even on large diffs.

A typical flow

  1. Look at the changed file in the diff and spot the different, independent changes.
  2. In the gutter, select only the lines that belong to the first thing.
  3. Stage the selection and write a commit that describes exactly that one thing.
  4. Repeat for the next independent change in the same file.
  5. Anything you do not want to keep at all, such as a debug print, you can discard on purpose.

In short

  • Staging whole files often mixes several changes.
  • Line staging builds self contained commits.
  • Such commits are easier to review and to undo.
  • In Spire you select the lines directly in the gutter.

Try Spire on your own repositories.

Free for 30 days, no account, no credit card. The native Git client you own instead of rent.