Staging Selected Changes for a Commit

The index lets one commit record a coherent change even when a file also contains other edits that belong elsewhere.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Choose a verifiable unit
  2. Watch dependencies between hunks
  3. Stage one coherent change while keeping nearby work visible
  4. Test the snapshot that will actually be shared

Choose a verifiable unit

Suppose one part fixes a count and another renames an unrelated method. If staged as separate hunks, the count fix can be committed and tested on its own. Review the staged diff before recording it.

Watch dependencies between hunks

If a chosen hunk calls a new helper from an unstaged hunk, the commit may fail even though the current working tree runs. Check the actual snapshot intended for the commit.[1]

Stage one coherent change while keeping nearby work visible

A function has a boundary fix and an unrelated message edit far below it. Select the boundary hunk for one commit and keep the message unstaged. Review the staged diff as a complete change before writing its commit message. See working tree and index.

Before: two unstaged hunks
Stage the boundary hunk
Staged diff: boundary only
Working diff relative to index: message only

Test the snapshot that will actually be shared

Running tests in the working tree can exercise unstaged code that the commit does not include. Where the split changes behavior, inspect or test a clean copy of the staged snapshot. Avoid separating a change from a test or support edit it requires.

References

  1. Scott Chacon and Ben Straub, Pro Git ↩