0

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?

Adam Dymitruk
  • 109,813
  • 21
  • 138
  • 137

1 Answers1

1

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.

Adam Dymitruk
  • 109,813
  • 21
  • 138
  • 137
  • I'm not sure I understand. I use `git add -p` and that starts showing me hunks where I will have various options (`y, n, q, a` etc.). I make my choice and then next hunk comes, until no more hunks are available, then I would `commit`. But the problem is that I'm not gonna `commit` since I only wanted this commit to add license, not comments. That's why I excluded everything but license. – Hichigaya Hachiman May 25 '17 at 01:48
  • it won't, it'll only add the hunks you said yes to. Next time around, the hunks you said yes to and committed, won't show up when you do the `git add -p`. You repeat this cycle until you have committed all your changes. – Adam Dymitruk May 25 '17 at 02:04
  • Ohhhh, so when I `cat` files and there are the changes already "applied", they have only been changed locally and I can split those changes into `commit`s, right? – Hichigaya Hachiman May 25 '17 at 02:29
  • Besides Adam's (correct) answer, you might want to read up on using `git diff --cached` to compare what's in the *index* right now (the selectively-staged parts) to what's in the `HEAD` (current) commit right now. That shows you what would be different in a commit you make *right now* with `git commit`, i.e., the difference between the HEAD files and the index files. – torek May 25 '17 at 07:32