Git rebase one branch on top of another branch

前端 未结 4 1496
栀梦
栀梦 2021-01-30 08:44

In my git repo, I have a Master branch. One of the remote devs created a branch Branch1 and had a bunch of commits on it. I branched from Branch1

4条回答
  •  无人及你
    2021-01-30 09:02

    First backup your current Branch2:

    # from Branch2
    git checkout -b Branch2_backup
    

    Then rebase Branch2 on Branch1:

    # from Branch2
    git fetch origin           # update all tracking branches, including Branch1
    git rebase origin/Branch1  # rebase on latest Branch1
    

    After the rebase your branch structure should look like this:

    master --
             \
              1 -- 2 -- 3 -- 4 -- Branch2'
    

    In the diagram above, the apostrophe on Branch2 indicates that every commit in the rebased Branch2 after commit 4 is actually a rewrite.

    Keep in mind that you have now rewritten the history of Branch2 and if the branch is already published you will have to force push it to the remote via

    git push --force origin Branch2
    

    Force pushing can cause problems for anyone else using Branch2 so you should be careful when doing this.

提交回复
热议问题