Search code examples
gitgit-rebasefeature-branch

Undo rebasing feature branch onto another feature branch


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?


Solution

  • 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.