Git 将一个分支变基到另一个分支之上

Git rebase one branch on top of another branch

在我的 git 仓库中,我有一个 Master 分支。其中一位远程开发人员创建了一个分支 Branch1 并在其上进行了一系列提交。我从 Branch1 分支出来,创建了一个名为 Branch2 (git checkout -b Branch2 Branch1) 的新分支,这样 Branch2 head 在添加到 Branch1 的最后一次提交上:(看起来像这个)

Master---
         \
          Branch1--commit1--commit2
                                   \
                                    Branch2 (my local branch) 

Branch1 发生了一些变化。另一个开发人员压缩了他的提交,然后又添加了一些提交。同时,我在我的分支中进行了一系列更改,但尚未提交任何内容。当前结构如下所示:

  Master---
             \
             Branch1--squashed commit1,2--commit3--commit4
                                       \
                                        Branch2 (my local branch)

现在我想在 Branch1 的基础上重新设置我的更改。我对如何去做这件事感到非常困惑。我知道第一步是使用 git add .git commit -m "message" 提交我的更改。但是我会推动吗?使用 git push origin Branch2 ?或 git push origin Branch2 Branch1 ?非常需要帮助,非常感谢,如果我能知道如何创建我的分支的备份,那将是很好的,以防我搞砸了

首先备份你当前的 Branch2:

# from Branch2
git checkout -b Branch2_backup

然后在 Branch1 上变基 Branch2:

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

变基后你的分支结构应该是这样的:

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

在上图中,Branch2 上的撇号表示变基 Branch2 after 提交 4 中的每个提交实际上都是重写。

请记住,您现在已经重写了 Branch2 的历史记录,如果分支已经发布,您将不得不通过

将其强制推送到远程
git push --force origin Branch2

强制推送可能会给其他使用 Branch2 的人带来问题,因此您在执行此操作时应小心。

git rebase branch1 branch2 会将分支 branch2 变基到 branch1。在操作上,这意味着任何仅包含在 branch2(而不是 branch1)中的提交都将在 branch1 之上重放,并移动 branch2 指针。有关详细信息,包括此操作的图表,请参阅 git rebase --help

该操作可能会产生一些冲突,您必须手动解决这些冲突。编辑受影响的文件、合并内容并删除任何失败的文件。之后,使用 git add <file> 将文件标记为已合并,然后使用 git rebase --continue 继续变基。重复直到完成。

一旦完成,您就没有其他事可做。你不必勉强。但是,如果您希望将您的新更改镜像到其他存储库(例如,与他人共享或将这些更改保存在您的另一个存储库中),请执行最后的 git push.

首先,您必须确保对 Branch1 的引用是最新的(特别是因为它的历史已被修改)。

如果你喜欢使用本地副本,你可以这样做:

git push origin Branch2 # this ensures you have at least one copy in your remote
git fetch origin
git checkout Branch1
git reset --hard origin/Branch1
git checkout Branch2
git rebase Branch1 # solve conflicts ... and check that everything is ok
git push -f origin Branch2

I want to rebase my changes (from local branch2) on top of branch1.

git checkout branch2   # Go to your local branch. Use -f to force the checkout.
git reset HEAD --hard  # Drop all non-committed changes.
git rebase branch1     # Rebase on top of branch1. Use -i for an interactive list.

注意:如果分支在远程,例如origin,请在分支名称前加上origin/

疑难解答

  • 如果您卡在了 rebase 中间并且想重新开始,运行:

    rm -fr .git/rebase-merge # Abort a rebase-merge mode.
    git reset HEAD --hard    # Reset everything to the current HEAD.
    
  • 如果您在分离分支上(运行: git branch 并寻找星号),运行:

    git checkout branch2 -f # and start again.
    
  • 如果发生冲突,您需要fix them,使用不同的变基点。

  • 如果您想一步一步地手动进行 rebase,请使用 cherry-picking。例如

    git reflog              # Note hashes of for your commits.
    git checkout master     # Go to your base branch.
    git cherry-pick C0MM1T1 # Cherry pick first commit based on its hash.
    # Go to the next one or solve the conflicts.
    git cherry-pick C0MM1T2 # Cherry pick another commit and so on.
    
  • 如果你的 rebase 在 运行ning git rebase branch1 -i 之后在交互式列表中显示太多提交,你可以在你的更改之前给定特定提交开始你的 rebase,例如git rebase pr3v1ios.