git 将更改从一个提交应用到另一个分支

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

git apply changes from one commit onto another branch

gitrebasecherry-pickgit-cherry-pick

提问by juniper-

I want to do something similar to git rebase but without collapsing parallel commits.

我想做一些类似于 git rebase 但不折叠并行提交的事情。

Let's say I have the following commits:

假设我有以下提交:

  B (bar)
 /
A-C-D (foo)

Now I want to take the changes that D introduced to C in branch foo, and apply them to B in branch bar. so that I end up with the following:

现在我想将 D 在分支 foo 中引入到 C 的更改应用到分支 bar 中的 B。所以我最终得到以下结果:

  B-E (bar)
 /
A-C-D (foo)

Where the difference between commits B and E is equal to difference between commits C and D. Is this possible? Is there a way to do it without creating a patch?

提交 B 和 E 之间的差异等于提交 C 和 D 之间的差异。这可能吗?有没有办法在不创建补丁的情况下做到这一点?

回答by cforbish

Yes:

是的:

git checkout -b mergebranch B
git cherry-pick D

回答by aaaarrgh

If the last commit on the branch that you want to cherry-pick out of (foo in the example) is a merge commit, you can point at the specific commit to cherry pick by using git cherry-pick branchname~1to get the commit which was the parent of the merge.

如果您想从中挑选出的分支上的最后一次提交(示例中的 foo )是合并提交,您可以通过使用git cherry-pick branchname~1获取作为合并父级的提交来指向樱桃挑选的特定提交.

回答by Patrick

In my case I needed to apply the changes of specific commits of another branch. I did that by cherry picking them like so: git cherry-pick COMMIT-HASH.

就我而言,我需要应用另一个分支的特定提交的更改。我这样做,通过采摘樱桃它们像这样: git cherry-pick COMMIT-HASH