THE GITHTML FIELD GUIDE
Stage part of a file with git add --patch
Use git add --patch to select individual change hunks instead of staging an entire file. Accept only hunks belonging to the next commit, split a hunk when Git can separate it, and inspect the staged diff afterward. Patch staging changes the index while leaving the complete edited file in your working tree.
Decide whether a partial commit is coherent
Patch staging is useful when one file contains two independent changes, such as a bug fix and a nearby wording cleanup. It is less useful when the selected lines depend on omitted changes to compile or behave correctly. The proposed commit must stand on its own, even though your working copy contains additional work that may make local tests pass.
Begin by reading the complete diff for the file. Identify which edits belong together and what tests demonstrate their behavior. If the logical changes are tangled in the same expression or function, separating the edits in your editor may be clearer than manually constructing a delicate patch. Keep a safe copy before restructuring complicated work.
Select hunks deliberately
The interactive prompt offers choices including accepting, skipping, splitting, and showing help. Read the options displayed by your installed Git rather than guessing a key. Splitting is available only when a hunk can be divided. Manual patch editing is powerful but requires understanding context and added or removed lines; beginners should prefer simpler selections.
- Use the staged diff to confirm the next commit contains the fix and its necessary supporting edits, not just the visually interesting lines.
- Use the remaining ordinary diff to confirm excluded work still exists. Partial staging should not be confused with discarding the unselected changes.
git diff -- parser.js
git add --patch -- parser.js
git diff --staged -- parser.js
git diff -- parser.jsVerify the snapshot rather than the editor view
A test run usually reads the working tree, which still contains both selected and unselected edits. Passing tests there does not prove that the staged snapshot works independently. For a high-risk split, inspect dependencies carefully and use the project’s established way to test an isolated commit after creating it. Do not silently remove unrelated work to create a clean test environment.
If the staged selection is wrong, unstage the affected path with git restore --staged -- parser.js, then repeat the selection; this assumes the repository already has a HEAD commit. After committing, inspect the recorded patch and the remaining status. The benefit of patch staging is reviewable intent, not maximum fragmentation. Two tiny commits that each break the program are worse than one coherent commit containing the necessary related changes.
A useful review question is whether a teammate could apply only this commit and understand the intended behavior without knowing about your uncommitted follow-up work.
Sources and further reading
AI-assisted writing with source-linked guidance and illustrative examples. Read our editorial approach or report a correction.