如何在 Git 中将一个分支的特定提交合并到另一个分支?

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

How do I merge a specific commit from one branch into another in Git?

gitgithub

提问by NullVoxPopuli

I have BranchAwhich is 113 commits ahead of BranchB.

我有BranchA113 次提交比BranchB.

But I only want the last 10 or so commits from BranchAmerged into BranchB.

但我只希望最后 10 个左右的提交BranchA合并到BranchB.

Is there a way to do this?

有没有办法做到这一点?

回答by ewall

The git cherry-pick <commit>command allows you to take a single commit (from whatever branch) and, essentially, rebase it in your working branch.

git cherry-pick <commit>命令允许您进行一次提交(来自任何分支),并且本质上是在您的工作分支中对其进行变基。

Chapter 5 of the Pro Git book explains it better than I can, complete with diagrams and such. (The chapter on Rebasingis also good reading.)

Pro Git 书的第 5 章比我能更好地解释它,并附有图表等。(关于 Rebase 的那一章也很好读。)

Lastly, there are some good comments on the cherry-picking vs merging vs rebasing in another SO question.

最后,在另一个 SO 问题中对挑选、合并和重新定位有一些很好的评论

回答by Seth Reno

If BranchA has not been pushed to a remote then you can reorder the commits using rebaseand then simply merge. It's preferable to use mergeover rebasewhen possible because it doesn't create duplicate commits.

如果 BranchA 尚未推送到远程,那么您可以使用rebase和重新排序提交,然后简单地merge. 最好尽可能使用mergeover rebase,因为它不会创建重复提交。

git checkout BranchA
git rebase -i HEAD~113
... reorder the commits so the 10 you want are first ...
git checkout BranchB
git merge [the 10th commit]

回答by artamonovdev

SOURCE: https://git-scm.com/book/en/v2/Distributed-Git-Maintaining-a-Project#Integrating-Contributed-Work

来源:https: //git-scm.com/book/en/v2/Distributed-Git-Maintaining-a-Project#Integrating-Contributed-Work

The other way to move introduced work from one branch to another is to cherry-pick it. A cherry-pick in Git is like a rebase for a single commit. It takes the patch that was introduced in a commit and tries to reapply it on the branch you're currently on. This is useful if you have a number of commits on a topic branch and you want to integrate only one of them, or if you only have one commit on a topic branch and you'd prefer to cherry-pick it rather than run rebase. For example, suppose you have a project that looks like this:

将引入的工作从一个分支转移到另一个分支的另一种方法是挑选它。Git 中的挑选就像是一次提交的变基。它采用提交中引入的补丁,并尝试在您当前所在的分支上重新应用它。如果您在一个主题分支上有多个提交并且只想集成其中一个,或者如果您在一个主题分支上只有一个提交并且您更愿意挑选它而不是运行 rebase,这将非常有用。例如,假设您有一个如下所示的项目:

enter image description here

enter image description here

If you want to pull commit e43a6 into your master branch, you can run

如果你想将提交 e43a6 拉入你的主分支,你可以运行

$ git cherry-pick e43a6
Finished one cherry-pick.
[master]: created a0a41a9: "More friendly message when locking the index fails."
 3 files changed, 17 insertions(+), 3 deletions(-)

This pulls the same change introduced in e43a6, but you get a new commit SHA-1 value, because the date applied is different. Now your history looks like this:

这会引入 e43a6 中引入的相同更改,但您会获得一个新的提交 SHA-1 值,因为应用的日期不同。现在你的历史看起来像这样:

enter image description here

enter image description here

Now you can remove your topic branch and drop the commits you didn't want to pull in.

现在您可以删除您的主题分支并删除您不想引入的提交。