【问题标题】:How to duplicate a branch into another existing branch in git如何将一个分支复制到 git 中的另一个现有分支
【发布时间】:2015-12-10 06:27:03
【问题描述】:

我想将一个分支中的所有更改推送到另一个分支(现有分支)而不合并。

例如,考虑两个分支 branch1 到 branch2。 branch1 和 branch2 分别跟踪 origin/branch1 和 origin/branch2。

Branch1 有提交 A,B,C,D,E,F Branch2 有提交 A,B,D,F

我想让 Branch2 与 branch1 完全一样。樱桃采摘和合并会产生冲突,我不想花时间解决,因为我要做的就是盲目地将分支 1 复制到分支 2。

我可以做到这一点

git checkout branch1 # Moves to branch1
git push origin :branch2 # Deletes remote branch origin/branch2
git branch -d branch2 # Deletes the local copy of this branch
git pull
git push origin HEAD:branch2 # Creates new branch in remote repository from the HEAD at local branch branch1

有没有更好的方法通过合并命令中的一些 --force 选项来做到这一点。我不想每次都删除分支只是为了创建一个具有相同名称的新分支。

谢谢

【问题讨论】:

  • 但是如果你的提交解决了冲突,那你到底为什么要盲目地把所有东西都扔进去?
  • 不会像git checkout branch2 git reset --hard branch1 这样的重置选项适合您吗?

标签: git github merge git-branch


【解决方案1】:
git switch branch1  
git pull  
git push origin branch1:branch2 --force-with-lease  #assume the remote name is origin  
git branch -f branch2 origin/branch2  #reset the local branch branch2 

【讨论】:

    【解决方案2】:
    git checkout branchA
    git merge -X theirs branchB
    

    【讨论】:

    【解决方案3】:

    从您要清理的分支开始,branch2 在您的情况下。然后,基于该分支并删除该分支唯一的所有提交。

    branch2 现在应该处于与您第一次创建它时相同的状态。现在要“克隆”来自branch1 的所有提交,只需将branch2 重新设置为branch1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-15
      • 2016-05-12
      • 2021-01-04
      • 2016-06-15
      • 2017-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多