Git and add -p problems

92 Views Asked by At

I have just applied a patch to 3 files, their diff can be seen here:

stack.h, stack.c and main.c.

The task is to separate the patch into various commits, like comments, license, refactoring etc.

For that I will use git add -p:

  1. First hunk is license of main.c, trivial, i press y.

  2. Second hunk is in stack.c and contains one hunk of license followed by two hunks of comments. I split and then y n n

  3. I will keep doing this for the rest of git add -p.

HOWEVER, it looks like none of the n applied!

To confirm my suspicions I will do a fresh git add -p again, this time I will use e and this will become this.

git then outputs fatal: corrupt patch at line 50. So basically my s and n tried to not stage certain hunks but ended up discarding those removals.

+ lines should be removed without problems unlike - (where you have to replace that line with space), right? What am I doing wrong?

1

There are 1 best solutions below

4
On

Looks like you did not follow git add -p + git commit at each step. Saying "no" with n means that the remaining changes haven't been staged. You need to repeat git add -p followed by git commit for each type of change that you want to appear in each commit.

HOWEVER, it looks like none of the n applied!

This is by design. It means you did not want that in the index. So the changes were not staged. When you commit, they will not be part of that commit. You must repeat the process to get /those/ in like the previous iteration.

You are done with git add -p + git commit iterations once there are no more outstanding changes to commit.

Again, the hunks you say "no" to with n will NOT be part of the commit you make with git commit.

It may be that you need to read up on how git's staging (or index) works.