Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I stage only selected lines of a file in Git?

Quick answer

Run git add -p path/to/file to review changes in that file and choose which to stage. If a displayed hunk mixes changes you want and changes you do not, try splitting it or editing it when those options are available. Then run git diff --cached and check that the staged diff contains only the changes intended for the next commit. [S1] [S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I edited one file for two different reasons, and I want the changes in separate commits. How do I stage just the lines for the first one?

Geez

Use interactive patch mode on that file:

git add -p path/to/file

Git presents changes as patch hunks and lets you choose which changes to stage. This gives you a chance to review changes before adding them. [S1]

For example, assume src/notes.txt has a wording fix and an unrelated new note. Start with git add -p src/notes.txt, and choose the wording change while leaving the new note unstaged. The example is illustrative: the actual hunk layout and choices depend on your edits. Don’t assume a hunk corresponds exactly to one line or one purpose; read the displayed change before choosing.

Follow-up question

What if the wording fix and the new note appear together in one hunk?

Geez

If the interactive prompt offers to split the hunk, try that first so you can select the smaller pieces separately. Git’s interactive patch mode also offers an edit option. [S1] Editing a patch requires care: inspect the prompt and resulting diff rather than assuming the edit selected exactly what you meant.

If you cannot confidently separate the changes, stop and review the patch instead of staging a mixed result. You can use interactive selection again after adjusting your working changes, but check what is staged afterward. The key is the result in the index, not merely the choice you intended to make.

Follow-up question

How can I check that the next commit will contain only the lines I selected?

Geez

After the selections, run:

git diff --cached

This shows changes staged for the next commit relative to HEAD by default. [S2] Read the whole displayed diff and confirm that the intended wording fix is present and the unrelated note is absent. This check is specifically about staged changes; it is not a general review of every unstaged working-tree change. [S2]

The interactive patch documentation also describes an interactive diff subcommand for reviewing the difference between HEAD and the index after selection. [S1] Either way, look at the proposed staged contents, not just the command’s completion.

Follow-up question

Can you give me a compact sequence and a way to think about the result?

Geez

Assuming you want the wording fix in the next commit and the new note kept out for now, use this sequence:

git add -p src/notes.txt
git diff --cached

At the prompts, select only the intended change; split or edit a mixed hunk if that helps. Then inspect the staged diff. If it contains extra lines, don’t proceed as though the selection were correct: review the staged result and make the selection again as needed.

A useful mental model is: interactive selection chooses changes for the index, and git diff --cached checks the proposed contents relative to HEAD by default. [S1] [S2] These commands help you review what is staged; they do not establish that the change itself is correct or that unstaged edits are ready. Once the staged diff matches your intent, continue with your normal commit workflow.

A practical checklist

  • Name the file and the specific change intended for the next commit.

  • Run git add -p path/to/file and read each displayed hunk before selecting.

  • Try splitting a mixed hunk; use the edit option only if you can review its result confidently.

  • Run git diff --cached after selection.

  • Confirm the staged diff includes the intended change and excludes unrelated lines.

A few more useful answers

Does `git diff --cached` show every change in my working tree?

No. It shows changes staged for the next commit relative to HEAD by default; it is a check of the index, not a general review of all unstaged working-tree changes. [S2]

Can interactive patch mode split a displayed hunk?

Yes. Git’s interactive patch mode offers options to split a hunk or edit it. [S1] Review the staged diff afterward to verify the result.

What if `HEAD` does not exist yet?

The Git documentation says git diff --cached shows all staged changes when HEAD does not exist. [S2]

Sources

References used for this guide.

  1. [S1] Git add: interactive patch staging
    git-scm.com · Checked
  2. [S2] Git diff: reviewing staged changes
    git-scm.com · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work