[英]How do I merge a specific commit from one branch into another in Git?
I have BranchA
which is 113 commits ahead of BranchB
. 我有
BranchA
,比BranchB
提前113次提交。
But I only want the last 10 or so commits from BranchA
merged into BranchB
. 但我只想将
BranchA
的最后10个提交合并到BranchB
。
Is there a way to do this? 有没有办法做到这一点?
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>
命令允许您进行单个提交(来自任何分支),并且实质上是在您的工作分支中对其进行rebase。
Chapter 5 of the Pro Git book explains it better than I can , complete with diagrams and such. Pro Git书的第5章比我更好地解释了它 ,完成了图表等。 ( The chapter on Rebasing is also good reading.)
( 关于重印的章节也很好。)
Lastly, there are some good comments on the cherry-picking vs merging vs rebasing in another SO question . 最后, 在另一个SO问题中,对于挑选与合并与变基有一些好评 。
If BranchA has not been pushed to a remote then you can reorder the commits using rebase
and then simply merge
. 如果BranchA尚未被推送到远程,那么您可以使用
rebase
重新排序提交,然后简单地merge
。 It's preferable to use merge
over rebase
when possible because it doesn't create duplicate commits. 在可能的情况下,最好使用
merge
over 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]
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.
Git中的一个樱桃选择就像是单一提交的基础。 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.
如果您在主题分支上有许多提交并且您只想集成其中一个提交,或者如果您只在主题分支上有一个提交,并且您更愿意选择它而不是运行rebase,那么这非常有用。 For example, suppose you have a project that looks like this:
例如,假设您有一个如下所示的项目:
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. 这引出了e43a6中引入的相同更改,但是您获得了新的提交SHA-1值,因为应用的日期不同。 Now your history looks like this:
现在您的历史记录如下:
Now you can remove your topic branch and drop the commits you didn't want to pull in. 现在,您可以删除主题分支并删除您不想提交的提交。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.