git 如何在git中将一个分支复制到另一个现有分支中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32561650/
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-19 11:23:17  来源:igfitidea点击:

How to duplicate a branch into another existing branch in git

gitgithubmergegit-branch

提问by akshitBhatia

I would like to push all the changes in one branch to another branch (existing branch) without merging.

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

For the example, consider two branches branch1 to branch2. Both branch1 and branch2 track origin/branch1 and origin/branch2 respectively.

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

Branch1 has commits A,B,C,D,E,F Branch2 has commits A,B,D,F

分支 1 有提交 A、B、C、D、E、F 分支 2 有提交 A、B、D、F

I would like to make Branch2 exactly like branch1. Cherry-picking and merging would give conflict which i dont wanna spend time resolving, because all i am trying to do is, blindly replicating branch1 into branch2.

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

I am able to do this by

我能够做到这一点

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

Is there a better way of doing this through some --force options in merge commands. I dont want to delete the branch everytime just to create a new branch with the same name.

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

Thanks

谢谢

回答by Mike Oiknine

Start on the branch you would like to clean, branch2in your case. Then, rebase on that branch and drop all the commits that are unique to that branch.

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

branch2should now be in the same state is it was when you first created it. Now to 'clone' all the commits from branch1, just rebase branch2onto branch1

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

回答by radiantRazor

git checkout branchA
git merge -X theirs branchB