Git Cheat Sheet · Changes

Stage part of a file

git add -p

When a file mixes two changes, git add -p offers you each block of changes one at a time: you choose the ones that go into the next commit. The rest of the file stays modified, but unstaged.

What it does

Stages only certain hunks or lines of a file, the rest staying unstaged.

A use case

The same file contains a fix and an ongoing experiment: you only want to commit the fix.

On the command line

git add -p <file>
Goes through the file's blocks one by one.
y / n
Stages this block, or leaves it out.
s
Splits the block into smaller blocks, when possible.
e
Opens the block in the editor to choose the lines by hand.
q
Stops here: what has already been accepted stays staged.

In Merathon

  1. Click the file to show its diff.
  2. "Stage hunk" takes a whole hunk.
  3. For a few lines, click the lines you want (Shift + click for a range) then "Stage lines".

Frequently asked questions

How do I stage a single line with Git?

On the command line, git add -p then e to edit the block. In Merathon, click the line in the diff, then "Stage lines".

How do I check what has been staged?

git diff --staged shows exactly what will go into the commit.

See also