I'm working on a develop branch and two different (local) feature branches.
a -- b -- e <-- develop
\ \
\ f -- g <-- feature-branch-1
\
c -- d <-- feature-branch-2
I incorporated the changes from feature-branch-1 into feature-branch-2 by running
git checkout feature-branch-2
git rebase feature-branch-1
If I understand it correctly, it now looks like this:
a -- b -- e <-- develop
|\
| f -- g <-- feature-branch-1
\
f -- g -- c -- d <-- feature-branch-2
However, I then realised that I introduced an error in branch 1 that I don't know how to fix yet. So this error is now in branch 2, too, and prevents me from merging feature-branch-2 into develop. I want to go back to the original state
a -- b -- e <-- develop
\ \
\ f -- g <-- feature-branch-1
\
c -- d <-- feature-branch-2
so that I can safely merge feature-branch-2 into develop. How can I achieve this?
Check out feature-branch-2
git checkout feature-branch-2
and rebase on the commits starting from g (last commit of feature-branch-1) onto develop:
git rebase --onto develop feature-branch-1
Note that feature-branch-2 will be based on e
instead of b
, but I do not think it matters much? If it does: replace develop
with b
in the git rebase
command.
You've got 2 possibilities:
git reflog feature-branch-2
to retrieve the hash of the commit d
.
And then undo by doing a reset (stash before running this command if you have uncommited changes):
git reset --hard <hash_of_commit_d>
This solution is equivalent to a pure "undo". You will be in the exact same state than before the rebase. Also, an advantage is that if you get conflict during the rebase, you won't have to solve them again in the other way.
edit: also described in this answer: https://stackoverflow.com/a/135614/717372
rebase --onto
(see @Veger answer for details)This solution is better if you did some new commit after the rebase. But if you had conflicts during the original rebase, you will have to solve the opposite ones.
Perhaps a good solution in this case is to do the reset
for the original commits and do the rebase --onto
only for the added commits (you will have to create a temporary branch to achieve that)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With