Git:将一个分支的代码覆盖到另一个分支 [英] Git: overwriting code of one branch to another

查看:1760
本文介绍了Git:将一个分支的代码覆盖到另一个分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个分支A和B.这两个分支都跟主人有很多不同的提交,主人已经提前。

我想要做的是用B替换B的所有更改。
即,B应该与A的代码完全相同。



我尝试使用rebase / merge。即使是他们在重新装订B时也是这样,但总是遇到大量冲突。



我试过了

  git checkout B 
git reset --hard A

如何将一个分支复制到另一个分支而不管更改?但这给了这个 -

 你的分支和'origin / B'有分歧,
有6603和1823不同的提交每个,分别。

有没有办法让我把A的代码/提交复制到B,没有冲突?

解决方案

  git checkout A -BB 

检查 A 的工作树,同时给它分支名称 B 。 (由于 B 已存在,所以需要 -B (big B))



实际上与此相同:

 #检查分支'A'并使工作树反映了这个
git checkout A

#在这个当前位置创建/覆盖分支'B'
git checkout -BB
指针)'A'和'B'指向完全相同的提交。



注意最终结果与您使用 git时的结果几乎相同结帐B&& git reset --hard A



现在,由于您好像关注 origin / B 已分散消息,您现在可以做两件事:

您可以更新 origin / B 以反映新的 B by force em> 推:(假设您不担心最初在 origin / B 中的提交将不复存在。)

  push origin B --force 


  • 出于某种原因,您无法强制推送(仅快速转发服务器?)或者您希望保留 B - 只是没有他们的变化。在这种情况下,它会涉及更多一些:(这个用例非常罕见,所以我不完全确定这是你要做的):

      git checkout A --detach 
    git merge -s our B
    git checkout -BB

    这会产生合并提交,该合并提交将 B 合并到 A ,而不实际更改任何文件。这个新的 B 可以正常推送,因为它不是与 origin / B 的分歧。



  • I have 2 branches A and B. Both have diverged from master with lots of different commits, and the master has moved ahead.

    What i want to do is replace all changes of B with A. i.e., B should have exactly the same code as A.

    I tried using rebase/merge. Even -Xtheirs while rebasing B, but always running into loads of conflicts.

    i tried

    git checkout B
    git reset --hard A
    

    as given in How to copy one branch to another regardless changes? but this gave this-

    Your branch and 'origin/B' have diverged,
    and have 6603 and 1823 different commits each, respectively.
    

    Is there a way i can get the code/commits of A copied to B, with no conflicts? Please help.

    解决方案

    git checkout A -B B
    

    Checkout the working tree of A, while giving it the branch name B. (Since B already exists, -B (big B) is required to override.)

    This is effectively the same as this:

    # Checkout the branch 'A' and make the working tree reflect this
    git checkout A
    
    # Create/override a branch 'B' at this current position
    git checkout -B B
    

    The end result is that the branches (which are practically just pointers) 'A' and 'B' point the the exact same commit.

    N.B. The end result of this is pretty much the same as what you had with git checkout B && git reset --hard A.

    Now, since you seem to be concerned with the origin/B has diversed message, there're two things you can do at this point:

    1. You can update origin/B to reflect your new B by force pushing: (This is provided you aren't worried that commits that were originally in origin/B will cease to exist.)

      push origin B --force
      

    2. For some reason, you're unable to force push (Fast-forward only server?) Or you want to preserve the commit history that was under B--just not their changes. In this case, it's a little more involved: (This use case is quite rare so I'm not entirely certain this is what you're after):

      git checkout A --detach
      git merge -s ours B
      git checkout -B B
      

      This makes a merging commit that proceeds A, that merges B into A without actually changing any files. This new B can be pushed normally as it's not a divergence from origin/B.

    这篇关于Git:将一个分支的代码覆盖到另一个分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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