I'd like to rebase to a specific commit, not to a HEAD of the other branch:
A --- B --- C master
\
\-- D topic
to
A --- B --- C master
\
\-- D topic
instead of
A --- B --- C master
\
\-- D topic
How can I achieve that?
I've used a mixture of solutions described above:
I found it much easier to read and understand. The accepted solution lead me to a merge conflict (too lazy to fix by hand):
A simpler solution is
git rebase <SHA1 of B> topic
. This works irrespective of where yourHEAD
is.We can confirm this behaviour from git rebase doc
You might be thinking what will happen if I mention SHA1 of
topic
too in the above command ?git rebase <SHA1 of B> <SHA1 of topic>
My little experiment shows that the command will execute but instead it will rebase a copy of
topic
branch there instead of rebasingtopic
branch. In short it does not work as we were expecting it to work. Hence don't use like this.You can even take a direct approach:
The comment by jsz above saved me tons of pain, so here's a step-by-step recipie based on it that I've been using to rebase/move any commit on top of any other commit:
git rebase --onto <new parent> <old parent>
In the example above that's as simple as:
There is another way of doing it or if you wish to move back to more than just one commit.
Here is a an example to move back to
n
number of commits:For the sake of this question, this can also be done:
The command works perfectly on
git version 2.7.4
. Haven't tested it on any other version.You can avoid using the --onto parameter by making a temp branch on the commit you like and then use rebase in it's simple form: