git is giving me a major headache when using --fixup and --autosquash. I would like to give two examples, one working perfectly fine and the other being a mess. (git version 2.
When you do a --fixup
, you are applying a patch out of order, so the context has disappeared. In the first case, your patches are applied as follows:
1
on line 1This is\naBUG
on lines 2, 3 after 1
a BUG
, on line 4, after This is
, replace with NOT a BUG
2
on line 2 after 1
, before This is
Steps 2, 3 are pretty clear-cut. Even though the line number is different than expected in step 3, the context makes it clear. In the second case,
1
on line 1This is a BUG
on line 2 after 1
This is a BUG
, replace with This is NOT a BUG
on line 3 after line 2
2
on line 2, after 1
, before This is a BUG
In this case, patch #3 is impossible because This is a BUG
does not appear on line 3 and the line before it is not 2
. Git does not assume that line 2 is the correct one in this case because of the missing context.
The easiest way to fix this problem is to rearrange the order of the rebase to reflect what you are actually doing. Instead of the original order:
pick 5ef0459 Added line 2 with BUG
fixup ed5cd81 fixup! Added line 2 with BUG
pick 20e104e Insert 2 --> second line
switch the last two elements to give the patch the context it needs:
pick 5ef0459 Added line 2 with BUG
pick 20e104e Insert 2 --> second line
fixup ed5cd81 fixup! Added line 2 with BUG
In this case, you may need to add the -k
flag to your command line to preserve the last commit, which is basically empty:
$ git rebase -i -k --autosquash HEAD~3
Date: Tue Nov 3 10:45:40 2015 -0500
1 file changed, 2 insertions(+), 1 deletion(-)
Successfully rebased and updated refs/heads/master.
$ cat test
1
2
This is NOT a BUG
The other alternative is of course to fix the conflict manually using git merge
or git mergetool
, following the prompts when the rebase fails.
You can make the rebase "succeed" by adding -s recursive -X theirs
or -s recursive -X ours
to specify the strategy. However, because of the context conflict, your fixup will get clobbered in both of those cases.