gitbranching-and-merging

git branch and merging from different branches


In my project I have a branch I used to work on called v.12 which has since deployed to prod. I created a feature branch based on v.12 after prod deployment, called v.12-fixes and had been working on it.

Since then, my colleague has created a new branch called v.13 based on v.12, but without v.12-fixes.

If I want to use v.13 and include the changes from v.12-fixes, what is the best way to do it?


Solution

  • In this situation, the best approach is to rebase your local branch v.12-fixes on top of v.13. This operation will rewrite every commit you have on the branch v.12-fixes, moving its base to the tip of v.13, and including all the changes from v.13 into v.12-fixes.

    To rebase your branch, first fetch the branch v.13 from the remote repository (I'm assuming its name is origin).

    git fetch origin v.13
    

    Then, rebase your local branch v.12-fixes on top of the tracking branch origin/v.13.

    # make sure to be on the branch to rebase
    git checkout v.12-fixes
    
    # rebase v.12-fixes on top of v.13
    git rebase v.13
    

    Warning: Before proceeding, if the local branch v.12-fixes has already been pushed to the remote, make sure that nobody else has based their work on it yet. As said before, rebasing re-writes the history, therefore if other developers have based their work on your current version of v.12-fixes, their changes would be based on a history that doesn't exist anymore after the rebase. If this is your case, coordinate first the rebase with the other developers to safeguard their work.