Undo rebasing feature branch onto another feature branch
Asked Answered
D

2

5

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?

Deviled answered 20/1, 2018 at 13:24 Comment(2)
You could try creating a new, empty branch from develop and use git cherry-pick to take only commits c and d.Lobule
With only two commits c and d worth keeping, this would be acceptable. If I were working on a long-running feature branch, picking commits individually would not be acceptable. I appreciate your help though!Deviled
S
7

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.

Subvention answered 20/1, 2018 at 13:28 Comment(3)
Shouldn't it be git rebase --onto b g?Electrotherapeutics
@Electrotherapeutics yes you are right (weird I was always using it as I wrote, but since today it is as you say... :o some bug in the git version I was using??) Thanks for pointing out!Subvention
The command from your answer "works" but produces different results than what the OP asked for. Rebasing onto the devel branch is common practice in many projects.Electrotherapeutics
T
4

You've got 2 possibilities:

  • The first and easier way is to look at the reflog:

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://mcmap.net/q/12967/-undoing-a-git-rebase

  • The second is to do a 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)

Thora answered 20/1, 2018 at 13:57 Comment(2)
If I had more commits in branch 2 after d, wouldn't that delete them? Say I incorporated branch 1 into branch 2, thus ended up with f-g-c-d, and then continued to commit h-i-j in branch 2 before I realised the error in branch 1. With your approach, wouldn't I end up with c-d again? What if I wanted to remove only the commits from branch 1 so that I'd end up with c-d-h-i-j in branch 2?Deviled
My solution strictly undo the rebase you made. The advantage is that you can't introduce a regression (especially if there was conflicts resolved during rebase). To do what you ask me, you have to do a rebase --onto described in the solution of @SubventionThora

© 2022 - 2024 — McMap. All rights reserved.