Simultaneously git add -p and git checkout -p

Viewed 542

I'm manually reviewing a huge number of changes (in the 1000s) made by a search and replace script over some message catalogs. At the moment I'm doing git add -p, but I keep taking breaks to check other files or adjust the script, so I'm alternating that with git checkout -p to discard the changes I don't want. Is there a way to combine the two? I.e. for each hunk I want the option to stage it, or discard it.

5 Answers

That is closer with Git 2.28 (Q3 2020)!
Before, "git checkout -p" did not handle a newly added path at all.

See commit 2c8bd84 (27 May 2020) by Johannes Schindelin (dscho).
(Merged by Junio C Hamano -- gitster -- in commit 2bdf00e, 09 Jun 2020)

checkout -p: handle new files correctly

Reported-by: Merlin Büge
Helped-by: Jeff King
Signed-off-by: Johannes Schindelin

The original patch selection code was written for git add -p, and the fundamental unit on which it works is a hunk.

We hacked around that to handle deletions back in 24ab81ae4d ("add-interactive: handle deletion of empty files", 2009-10-27, Git v1.6.6-rc0 -- merge).
But git add -p would never see a new file, since we only consider the set of tracked files in the index.

However, since the same machinery was used for git checkout -p & friends, we can see new files.

Handle this case specifically, adding a new prompt for it that is modeled after the deleted file case.

This also fixes the problem where added _empty_ files could not be staged via git checkout -p.

So the prompt for git checkout -p now includes:

Discard mode change from worktree [y,n,q,a,d%s,?]?
Discard deletion from worktree [y,n,q,a,d%s,?]?
Discard addition from worktree [y,n,q,a,d%s,?]?     <===
Discard this hunk from worktree [y,n,q,a,d%s,?]?

If you discard what you don't want, and commit what you need, then a git add $(git ls-files -o --exclude-standard) will add the new remaining files.

Related