2

So I have a problem adding hunks for my commit properly.

I have something like so:

@@ -6,6 +9,5 @@
#include "incfile.h"

 bool func3(int&, const char*);
-int func1(const int, std::vector<std::unique_ptr<type> >&);
-void func2(const std::vector<std::unique_ptr<type> >&);
+int func1(const int, std::vector<std::shared_ptr<type> >&);
 void func4(const int);

I am trying to only stage only the change to func2 I want t he changes to func1 in a diff commit, and I am struggling because every time and every combination of me removing - or lines or whatever is resulting in a patch that doesn't apply. I thought that I could simply remove the - from in front of func1 and delete the + line for func1 and it would be fine but it is not working.

csteifel
  • 2,854
  • 6
  • 35
  • 61

2 Answers2

1

Do it like this:

 int func1(const int, std::vector<std::unique_ptr<type> >&);
-void func2(const std::vector<std::unique_ptr<type> >&);

In other words, replace the first - with a (space) and completely delete the last line.

Agis
  • 32,639
  • 3
  • 73
  • 81
  • I have tried that and it still says the patch doesn't apply – csteifel Feb 27 '14 at 19:59
  • Please give the exact error. Also what command do you execute to stage the file? – Agis Feb 27 '14 at 20:20
  • How exactly are you trying to stage the file? – Agis Feb 27 '14 at 20:33
  • I'm doing a `git add -p file` and I have 3 chunks the first one I am not adding this is the second and the third I am not adding. – csteifel Feb 27 '14 at 20:37
  • And you press 'e' at the second chunk? Also, is the third chunk relevant to the second? – Agis Feb 27 '14 at 20:43
  • I press e on the second chunk yes and no the third is separate and has no relation to the second – csteifel Feb 27 '14 at 20:44
  • Can you try pressing 's' when the second chunk appears and apply each line as you want? – Agis Feb 27 '14 at 20:46
  • It will not split it into individual lines. I however fixed it I had to edit the file as a whole and do as you said and it worked fine but for some reason because I was doing it in the split it wouldn't work – csteifel Feb 27 '14 at 20:48
0

You could try git rebase -i ... to set up a range of commits for mangling. One of the options the -i (interactive) flag gives on each commit is to edit it. That makes the process stop, so you can change the commit at leisure (in this case, editing the modified file to taste), then git add incfile.h to record the change, and git rebase --continue to replay the other commits.

If you are paranoid, you could create a branch off the tip to fool around and don't risk losing it by git checkout -b experiment and work on the new experiment branch.

vonbrand
  • 11,412
  • 8
  • 32
  • 52