Skills › 💻 Terminal & git

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.

⏱ 45s

  1. Run git add -p (or git add -p file.txt for one file).
  2. Git shows one change (a hunk) at a time.
  3. Press y to stage it or n to skip it, then Enter.
  4. Press s to split a big hunk into smaller ones, or q to stop.
  5. 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.

Practice in the feed →

Next crumbs

Sources