Stage only part of a file with git add -p
Fixed a bug and tidied something unrelated in the same file? Commit them separately, hunk by hunk.
- Run
git add -p(orgit add -p file.txtfor one file). - Git shows one change (a hunk) at a time.
- Press
yto stage it ornto skip it, then Enter. - Press
sto split a big hunk into smaller ones, orqto stop. - Commit as usual: only the hunks you said yes to go in.
Tip: Run git diff --staged before committing to see exactly what you picked.
Try it now: Make two small edits in different parts of a file in any repo, run
git add -p, stage one and skip the other, then check git status.Next crumbs
- 💻 Terminal & gitSee exactly what you're about to commitPlain git diff hides anything you've already staged. Add one flag to see what the commit will contain.
- 💻 Terminal & gitFix your last commit with --amendForgot a file or made a typo in the message? Fold the fix into the commit you just made.
- 💻 Terminal & gitJump back with cd -Toggle between your last two directories, like Alt+Tab for folders.