Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit Git "add patch" hunks/diffs/lines during selective staging?

I have a source file where 2 features have been added. In order to allow cherry-picking, I'd like to commit that in 2 phases: one for each feature. Until now, in similar situations, using git add -p served me well, to commit one feature while leaving the local files in their final stage.

However, I now have the problem that git add -p wants to stage a hunk that includes edits for both features. Even though the edits are on separate lines, s (for "split") no longer wants to split up the hunk into smaller pieces...

In short: I can't separate the changes for the 2 features this way. Is there a way to manually edit the patch, for example using vi, without actually changing the original file?

like image 663
bart Avatar asked Feb 25 '10 12:02

bart


People also ask

What is git add-- patch?

What is “git add -patch” git add -p is short for git add --patch and it is a git option that allows you to make more specific commits. How it works is that it will go through all new changes in your code and display hunks of them at a time for you to decide what you would like to stage or not stage.

What are hunks in git?

When you enter Git's patch mode, the chunks of code ('hunks') you're offered to stage/skip can sometimes be bigger than you'd want. Maybe a hunk you're offered contains multiple lines with changes that belong in more than one commit. Luckily, the s option is there to split the hunk down further.

What is git add command?

The git add command adds a change in the working directory to the staging area. It tells Git that you want to include updates to a particular file in the next commit. However, git add doesn't really affect the repository in any significant way—changes are not actually recorded until you run git commit .


2 Answers

As Alan says, edit the patch by pressing e (instead of s) during git add -p. This will launch your editor with that hunk of the patch so that you can manually edit it. There are comments within the text that explain how to properly discard modifications and it's actually pretty easy.

When you are done, note that you can test it with only the changes you've just added by doing git stash --keep-index. The changes you did not add to the index will be stashed away and you are now free to test just the changes that you are about to commit. When done, simply git stash pop or git stash apply to get the other changes back.

like image 127
Dan Moulding Avatar answered Oct 17 '22 07:10

Dan Moulding


Like other people have said, you can use e to edit the hunk you want to split up.

To only add a portion of the hunk, you can delete the lines from the change you want to split out.

+Line 1 +Line 2 +Line 3 

Lets say you want to keep Line 1 and Line 3 in one commit and Line 2 in another. All you have to do is delete Line 2:

+Line 1 +Line 3 

This will put Line 1 and Line 3 in your staging area. Line 2 will still be indexed but not staged.

like image 32
Kousha Avatar answered Oct 17 '22 06:10

Kousha