Git在另一个分支之上重新分配一个分支 [英] Git rebase one branch on top of another branch

查看:144
本文介绍了Git在另一个分支之上重新分配一个分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 大师--- 
\
Branch1 - commit1 - commit2
\
Branch2(我的本地分支)

Branch1 进行了一些更改。另一个开发人员压缩了他的提交,然后增加了几个提交。与此同时,ive在我的分支有一些变化,但还没有做任何事情。当前结构如下所示:

 大师 -  
\
Branch1 - 压扁的commit1, 2 - commit3 - commit4
\
Branch2(我的本地分支)



<现在我想要在 Branch1 之上重新定义我的更改。我对如何去做这件事感到极度困惑。我知道第一步将使用 git add。 git commit -m消息来提交我的更改。但是,然后我推?使用 git push origin Branch2 ?或 git push origin Branch2 Branch1 ?帮助是非常需要和非常感谢,也是如果我可以如何创建我的分支的备份,这将是伟大的情况下,我拧了一些东西了解决方案

/ div>

首先备份你目前的 Branch2

 #从Branch2 
git checkout -b Branch2_backup

然后重新分配 Branch2 Branch1

 #来自Branch2 
git fetch origin#更新所有跟踪分支,包括Branch1
git rebase origin / Branch1#rebase on latest Branch1

重新绑定后,您的分支结构应该如下所示:

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

在上面的图表中, Branch2 上的撇号表示在提交之后重新分配的 Branch2 中的每个提交4实际上是一个重写e。

请记住,您现在已改写 Branch2 的历史记录,并且如果分支已发布将不得不强制推送到远程通过

  git push --force origin Branch2 

使用 Branch2 时,强制推送可能会给其他人造成问题,所以您在做时应该小心这个。

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, creating a new branch called Branch2 (git checkout -b Branch2 Branch1) such that Branch2 head was on the last commit added to Branch1:(Looks like this)

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

Branch1 has 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:

  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

解决方案

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.

这篇关于Git在另一个分支之上重新分配一个分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆