如何在不合并的情况下将提交从一个分支复制到 git 中的另一个分支?

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

How to copy a commit from one branch to another in git without any merging?

gitcopybranchcommit

提问by reubenjohn

I have 2 branches a master and an experimental. A shown:

我有 2 个分支,一个主分支和一个实验分支。A 显示:

master-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-Y
                  \
                   -x-x-x-x

My experimental is quite outdated and i was hoping to update it by simply copying the last commit in the master branch (Y) to experimental:

我的实验已经过时了,我希望通过简单地将主分支(Y)中的最后一次提交复制到实验来更新它:

master-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-Y
                  \
                   -x-x-x-x-Y

If possible, I don't want to have to do any merging I want to overwrite anything in the experimental (master is my main priority).

如果可能,我不想进行任何合并,我想覆盖实验中的任何内容(master 是我的主要优先事项)。

Edit:Let me briefly explain the situation: When i try to merge commits at the tips of the master and experimental branch, I get a lot of merge conflicts! The same happens if i try to cherry-pick from the experimental onto the master! I was hoping to avoid them as i simply don't want any of the changes on the experimental! Up until now, I have been cherry-picking from master to experimental and when there are merge conflicts, I just keep changes of master branch. But after doing it many times, i was hoping that there may be some way in which i can do something like a merge except where (i am not prompted with any merge conflicts as master changes is all i need (for all I know it wouldn't matter what was previously on the experimental branch!

编辑:让我简单解释一下情况:当我尝试在 master 和experimental 分支的提示处合并提交时,我遇到了很多合并冲突!如果我尝试从实验中挑选到主人,也会发生同样的情况!我希望避免它们,因为我根本不希望对实验进行任何更改!到目前为止,我一直在从 master 到实验性的挑选中,当出现合并冲突时,我只保留 master 分支的更改。但是在做了很多次之后,我希望可能有某种方式我可以做一些像合并一样的事情,除了 where(我没有被提示任何合并冲突,因为我只需要主更改(据我所知)之前在实验分支上的内容无关紧要!

采纳答案by reubenjohn

Here is what I initially tried!:

这是我最初尝试的!:

git checkout experimental
git rebase --onto <COMMIT ID OF LAST MASTER COMMIT> experimental

On trying this code, I found that not only the commit at the tip of the master butalso the entire experimental branch became a clone of the master! Yes, it is not an elegant solution and a bad usage of git!

在尝试这段代码时,我发现不仅是master尖端的commit,而且整个实验分支都变成了master的克隆!是的,这不是一个优雅的解决方案,也不是 git 的错误用法!

However, as pointed out by @Johnsyweb, the optimum solution would be to do a merge of experimental and master but,

然而,正如@Johnsyweb 所指出的,最佳解决方案是将实验和大师合并,但是,

with Git preferring to take the changes on master over the changes on experimental

与 Git 更喜欢在 master 上进行更改而不是对实验的更改

Hence doing a:

因此做一个:

git checkout experimental
git merge -Xtheirs master

should work fine. Although it says merge, since it only considers the changes of the experimental branch over the master one, it is more like a copy from experimental to master for all practical purposes.
(See a similar discussion here)

应该工作正常。虽然说是merge,但是因为它只考虑了实验分支对master分支的变化,所以它更像是一个从实验到master的副本,用于所有实际目的。
(请参阅此处的类似讨论)

回答by Johnsyweb

To cherry-pickjustcommit Yfrom masteronto experimental:

选择Ymasteron提交experimental

git checkout experimental
git cherry-pick Y

Alternatively:

或者:

git checkout experimental
git cherry-pick master

...Will apply the change introduced by the commit at the tip of the masterbranch and create a new commit in experimentalwith this change.

...将应用由master分支尖端的提交引入的更改,并experimental使用此更改创建一个新的提交。