简体   繁体   English

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

[英]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.

相关问题 如何将两个具有特定提交ID的Git分支合并到一个分支中? - How do I merge two Git branches, with specific commit ids, into one branch? 如何将一个分支中的特定文件合并到Git中的另一个分支中 - How can I merge a specific file from one branch into another branch in Git GIT:我如何从一个分支到另一个分支进行部分合并? - GIT: how do i do a partial merge from one branch to another one? 使用 git,如何从另一个分支中的文件(绕过合并)强制覆盖一个分支中的文件并提交? - With git, how to force overwrite a file in one branch, from a file in another branch (bypass merge), and commit? 如何在一个git commit中将master分支的更改合并到我的side project分支中? - How do I merge a master branch's changes into my side project branch in one git commit? 如何在保留提交历史记录的同时将代码块从一个git分支复制到另一个分支? - How do I copy a block of code from one git branch to another, while retaining commit history? 如果我从另一个分支推送,是否会在 git 分支上提交? - Will a commit on a git branch follow if I do a push from another branch? 通过特定的提交隔离git从一个分支到另一个分支的更改 - Isolate git changes from one branch to another by specific commit 如何将提交从一个分支分配到另一个分支? - How do I assign a commit from one branch to another? 如何将1个提交从分支合并到另一个分支 - how can I merge 1 commit from a branch to another branch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM