Omnikar
Omnikar

Reputation: 337

Git: Manually edit diff of whole file for selective staging?

I often use git add --patch in order to stage only select changes to a file; but sometimes, I have a file where the changes I want to stage are very mixed with the changes I don't want to stage. I've been using the feature of manually editing a diff hunk before staging it, but it can get a bit annoying to edit each hunk one at a time. Is there a way to manually edit a diff of the whole file and then stage it instead of each individual hunk?

Upvotes: 2

Views: 1885

Answers (1)

torek
torek

Reputation: 488123

Run git diff with whatever options you like (e.g., the specific file in question), redirect the output to a file, edit the file, and run git apply --cached --recount on the patch file. Note that this is different from git apply --index, which tries to patch both the working tree file and the index copy.

Personally, I don't like to do this sort of thing. Instead, I just move the file out of the way (e.g., foo.c becomes foo.c+), restore the current index copy, edit that in my editor until I have the one I want to have in the index, and then git add that one. After committing, I can then mv foo.c+ foo.c. The issue I have here is that the index copy is not easily visible, and I like to see exactly what I am going to commit, rather than shadows of differences between what I am going to commit, and what I have in my working tree.

Upvotes: 4

Related Questions