Search code examples
gitgit-mergegit-rebase

Rebase already merged branch on top of master


I got the following problem. I have two Git branches (First: Commits E, C, B and Second: Commit D) as seen on the picture beeing forked from and merged into the master branch (F and A)

Git Tree

I would like to put D at the top of A and remove it from the place it is currently located. Is this possible somehow?


Solution

  • The only way I see is to reset master to F, reset yellow branch to E, cherry-pick C and B on yellow branch, merge yellow into master and finally cherry-pick D.

    You should create a backup branch in the state of your current master before touching anything, to be able to reset to the current state if anything goes wrong, and to have easy access to the commit IDs you need to cherry-pick. The command sequence would be something like this:

    #on master branch, with no unstaged or uncommited changes:
    git branch backup
    git reset --hard F
    git checkout yellow
    git reset --hard E
    git cherry-pick C
    git cherry-pick B
    
    #merge yellow branch into master, however you usually do that
    
    git checkout master
    git cherry-pick D
    

    Warning: not tested.

    As I said in my comment, this will mess things up badly if anyone is already working on top of the old master.

    EDIT: If C is the merge commit from D to yellow, it should oviously not be cherry-picked and can be ignored.