Lets say I have a branch based on master which ends with merge commit:
--A---C<master]---D---E---F<my-feature]
\ /
B
Adding a commit X
between D
and E
with interactive rebase is simple - I run
git rebase -i master
, set commit D
operation to edit
, rebase stops after rebasing D
. I just create the new commit at that moment and then continue rebase. The branch now looks like
...C<master]---D---X---E'---F'<my_freature]
.
Now I want to add a commit between C
and D
.
Desired result:
C<master]---X---D'---E'---F'<my_freature]
I tried
git rebase -i master~
where I wanted to set the merge commit C
to edit
, but interactive rebase somehow ignores the merge commit C
and offers me only chain A--B--D--E
, so the rebase results in loss of merge commit C
.
Is there a simple way prepend a commit to a branch with interactive rebase like this?
Please note that I can figure a bit more complex solution like creating new branch tmp
on master
, committing X
to it then rebase my-feature
onto tmp
, I'm just curious if there is a simple and straightforward way with interactive rebase.
I have just successfully reproduced your scenario and inserted commit X between C and D by adding the -p
option (--preserve-merges
) to `git rebase:
git rebase -i -p master^
the rest of the workflow is as you describe: tell rebase to edit the merge commit, manually insert the new one and finish with git rebase --continue
.
Cool workflow, by the way! I think I'll use it.
Edit: I tested this solution with Git 2.4. If you have a more recent version of Git, @torek recommends using the safer --rebase-merges
option instead of --preserve-merges
. See the comments below for the explanation.