Search code examples
gitmergerevert

How can I drop a branch from a mulitple branch merger in git?


I just merged three different branches together as part of a feature set and pushed the results so that the repository tree currently looks like the following:

--- A --- A1 --- A2 --- A3 --
    |                         \
    + --- B1 --- B2 --- B3 --- B4-
    |                              \
    + --- C1 --- C2 --- C3 ---------C4

However, I have just been told that the B branch as not ready for prime time so I need to undo those commits. How can I roll back my changes so that my new head C5 would be a merger of the C3 and A3 commits? When I am done I want the repository to look like the following:

    + --- B1 --- B2 --- B3 --- B4 --- B5
    |                         /
--- A --- A1 --- A2 --- A3 --
    |                         \
    + --- C1 --- C2 --- C3 --- C4 --- C5

Solution

  • # checkout before merge
    git checkout C3
    # make merge we want
    git merge A3
    # rebase any work on top of the new merge
    git rebase --onto HEAD C4 branch_C
    # use fixed version as branch_C
    git branch -f branch_C HEAD
    # aha! now you on the fixed branch_C
    git checkout branch_C
    

    Than do exactly the same with B. I suppose you didn't branch C & B further after 'bad' merge.