Git 将一个分支重新绑定到另一个分支之上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40009820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:33:33  来源:igfitidea点击:

Git rebase one branch on top of another branch

gitrebase

提问by Beginner

In my git repo, I have a Masterbranch. One of the remote devs created a branch Branch1and had a bunch of commits on it. I branched from Branch1, creating a new branch called Branch2(git checkout -b Branch2 Branch1) such that Branch2head was on the last commit added to Branch1:(Looks like this)

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

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

Branch1has had a number of changes. The other dev squashed his commits and then added a few more commits. Meanwhile, ive had a bunch of changes in my branch but havent committed anything yet. Current structure looks like this:

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

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

Now I want have to rebase my changes on top of Branch1. I am supremely confused on how to go about this. I know the 1st step will be to commit my changes using git add .and git commit -m "message". But do i then push? using git push origin Branch2? or git push origin Branch2 Branch1? Help is much needed and GREATLY appreciated, also if I can some how create a backup of my branch, it will be great in case I screw something up

现在我想在Branch1. 我对如何解决这个问题感到非常困惑。我知道第一步是使用git add .and提交我的更改git commit -m "message"。但是我会推动吗?使用git push origin Branch2? 或者git push origin Branch2 Branch1?非常需要帮助,非常感谢,如果我能知道如何创建我的分支的备份,万一我搞砸了,那就太好了

回答by Tim Biegeleisen

First backup your current Branch2:

首先备份您当前的Branch2

# from Branch2
git checkout -b Branch2_backup

Then rebase Branch2on Branch1:

然后Branch2基于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 Branch2indicates that every commit in the rebased Branch2aftercommit 4 is actually a rewrite.

在上图中,撇号 onBranch2表示Branch2提交 4之后的 rebase 中的每个提交实际上是一次重写。

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

请记住,您现在已经重写了历史,Branch2如果分支已经发布,您将不得不通过以下方式将其强制推送到远程

git push --force origin Branch2

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

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

回答by dkasak

git rebase branch1 branch2will rebase branch branch2onto branch1. Operationally, this means any commits which are contained only in branch2(and not in branch1) will be replayed on top of branch1, moving the branch2pointer with them. See git rebase --helpfor more information, including diagrams of this operation.

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

The operation might produce some conflicts which then you'll have to resolve manually. Edit the affected files, merging content and removing any failed hunks. Afterwards, mark the files as merged using git add <file>and then continue the rebase using git rebase --continue. Repeat until it is done.

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

Once done, you have nothing else to do. You don't have to push. However if you wish to mirror your new changes to some other repository (for instance, to share it with others or to have those changes in another repository of yours), do a final git push.

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

回答by kenorb

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

我想branch2branch1.

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.

Note: If a branch is on the remote such as origin, prefix the branch name with origin/.

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

Troubleshooting

故障排除

  • If you got stuck in the middle of rebaseand you want to start over, run:

    rm -fr .git/rebase-merge # Abort a rebase-merge mode.
    git reset HEAD --hard    # Reset everything to the current HEAD.
    
  • If you're on the detached branch (run: git branchand look for the star symbol), run:

    git checkout branch2 -f # and start again.
    
  • If you get conflicts, you need to fix them, use different rebasing point.

  • If you'd like to do rebase manually step by step, use cherry-picking. E.g.

    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.
    
  • If your rebase showing too many commits on the interactive list after running git rebase branch1 -i, you can start your rebase given the specific commit just before your changes, e.g. git rebase pr3v1ios.

  • 如果您被卡在中间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.
    
  • 如果遇到冲突,则需要修复它们,使用不同的变基点。

  • 如果您想逐步手动进行变基,请使用樱桃采摘。例如

    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.
    
  • 如果您的变基在运行后在交互式列表上显示太多提交git rebase branch1 -i,您可以在更改之前给定特定提交开始变基,例如git rebase pr3v1ios.

回答by Tiba

First of all, you have to make sure your reference to Branch1 is up to date (specialy since it's history has been modified).

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

If you like to work with local copys, you cand do something like this:

如果您喜欢使用本地副本,可以执行以下操作:

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