用 Git 挑选提交意味着什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9339429/
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
What does cherry-picking a commit with Git mean?
提问by Rahul
Recently, I have been asked to cherry-pick
a commit.
最近,我被要求cherry-pick
提交。
So what does cherry-picking a commit in git mean? How do you do it?
那么在 git 中挑选提交意味着什么?你怎么做呢?
回答by Philip Fourie
Cherry picking in Git means to choose a commit from one branch and apply it onto another.
Git 中的樱桃采摘意味着从一个分支中选择一个提交并将其应用到另一个分支上。
This is in contrast with other ways such as merge
and rebase
which normally apply many commits onto another branch.
这与其他方式形成对比,例如merge
和rebase
通常将许多提交应用于另一个分支。
Make sure you are on the branch you want to apply the commit to.
git checkout master
Execute the following:
git cherry-pick <commit-hash>
确保您位于要应用提交的分支上。
git checkout master
执行以下操作:
git cherry-pick <commit-hash>
N.B.:
注意:
If you cherry-pick from a public branch, you should consider using
git cherry-pick -x <commit-hash>
This will generate a standardized commit message. This way, you (and your co-workers) can still keep track of the origin of the commit and may avoid merge conflicts in the future.
If you have notes attached to the commit they do not follow the cherry-pick. To bring them over as well, You have to use:
git notes copy <from> <to>
如果你从公共分支挑选,你应该考虑使用
git cherry-pick -x <commit-hash>
这将生成标准化的提交消息。这样,您(和您的同事)仍然可以跟踪提交的来源,并可能避免将来发生合并冲突。
如果您在提交上附加了注释,则它们不会遵循优先选择的原则。要将它们也带过来,您必须使用:
git notes copy <from> <to>
Additional links:
附加链接:
回答by Teoman shipahi
This quote is taken from; Version Control with Git(Really great book, I encourage you to buy it if you are interested in git)
此引文摘自; 使用 Git 进行版本控制(非常好的书,如果您对 git 感兴趣,我鼓励您购买它)
Edit: Since this answer is still getting impression, I would like to add very nice in action video tutorial about it:
编辑:由于这个答案仍然令人印象深刻,我想添加关于它的非常好的动作视频教程:
Youtube: Introduction to Git cherry-pick
Using git cherry-pick The command git cherry-pick commit applies the changes introduced by the named commit on the current branch. It will introduce a new, distinct commit. Strictly speaking, using git cherry-pick doesn't alter the existing history within a repository; instead, it adds to the history. As with other Git operations that introduce changes via the process of applying a diff, you may need to resolve conflicts to fully apply the changes from the given commit. The command git cherry-pick is typically used to introduce particular commits from one branch within a repository onto a different branch. A common use is to forward- or back-port commits from a maintenance branch to a development branch.
使用 git cherry-pick 命令 git cherry-pick commit 在当前分支上应用由命名提交引入的更改。它将引入一个新的、独特的提交。严格来说,使用 git cherry-pick 不会改变存储库中的现有历史;相反,它增加了历史。与通过应用差异的过程引入更改的其他 Git 操作一样,您可能需要解决冲突以完全应用来自给定提交的更改。命令 git cherry-pick 通常用于将特定提交从存储库中的一个分支引入到不同的分支。一个常见的用途是将提交从维护分支向前或向后移植到开发分支。
$ git checkout rel_2.3
$ git cherry-pick dev~2 # commit F, above
before:
前:
after:
后:
回答by Tadeck
Cherry picking in Git is designed to apply some commit from one branch into another branch. It can be done if you eg. made a mistake and committed a change into wrong branch, but do not want to merge the whole branch. You can just eg. revert the commit and cherry-pick it on another branch.
Git 中的樱桃采摘旨在将一些提交从一个分支应用到另一个分支。如果你例如,它可以完成。犯了一个错误并将更改提交到错误的分支,但不想合并整个分支。你可以只是例如。恢复提交并在另一个分支上挑选它。
To use it, you just need git cherry-pick hash
, where hash
is a commit hash from other branch.
要使用它,您只需要git cherry-pick hash
,hash
来自其他分支的提交哈希在哪里。
For full procedure see: http://technosophos.com/2009/12/04/git-cherry-picking-move-small-code-patches-across-branches.html
有关完整程序,请参阅:http: //technosophos.com/2009/12/04/git-cherry-picking-move-small-code-patches-across-branches.html
回答by Daniel Perník
Short example of situation, when you need cherry pick
情况的简短示例,当您需要挑选时
Consider following scenario. You have two branches.
a) release1- This branch is going to your customer, but there are still some bugs to be fixed.
b) master- Classic master branch, where you can for example add functionality for release2.
考虑以下场景。你有两个分支。
a) release1- 此分支将发送给您的客户,但仍有一些错误需要修复。
b) master- 经典的 master 分支,例如您可以在其中添加 release2 的功能。
NOW: You fix something in release1. Of course you need this fix also in master. And that is a typical use-case for cherry picking. So cherry pick in this scenario means that you take a commit from release1branch and include it into the masterbranch.
现在:您修复了release1 中的某些内容。当然,您也需要在master 中进行此修复。这是樱桃采摘的典型用例。因此,在这种情况下,挑选意味着您从release1分支进行提交并将其包含到主分支中。
回答by Vijay S B
cherry-pick is a Git feature. If someone wants to Commit specific commits in one branch to a target branch, then cherry-pick is used.
git cherry-pick
steps are as below.
cherry-pick 是 Git 的一个特性。如果有人想将一个分支中的特定提交提交到目标分支,则使用cherry-pick。
git cherry-pick 步骤如下。
- checkout (switch to) target branch.
git cherry-pick <commit id>
Here commit id is activity id of another branch.Eg.
git cherry-pick 9772dd546a3609b06f84b680340fb84c5463264f
- push to target branch
- 结帐(切换到)目标分支。
git cherry-pick <commit id>
这里提交 id 是另一个分支的活动 id。例如。
git cherry-pick 9772dd546a3609b06f84b680340fb84c5463264f
- 推送到目标分支
回答by MarianD
I prepared step-by-step illustrations what cherry-pickdoes — and an animation of these illustrations(near the end).
我准备了cherry-pick所做的分步插图- 以及这些插图的动画(接近尾声)。
- Starting the command
git cherry-pick feature~2
(feature~2
is the 2ndcommit beforefeature
, i.e. the commitL
):
Note:
笔记:
The commit L'
is from the user's point of view (commit = snapshot)the exact copy of the commit L
.
提交L'
是从用户的角度(提交 = 快照)提交的精确副本L
。
Technically (internally), it's a new, differentcommit (because e.g. L
contains a pointer to K
(as its parent), while L'
contains a pointer to E
).
从技术上讲(在内部),它是一个新的、不同的提交(因为例如L
包含一个指向K
(作为其父级)L'
的指针,同时包含一个指向 的指针E
)。
回答by Hugh
You can think if a cherry pick as similar to a rebase, or rather it's managed like a rebase. By this, I mean that it takes an existing commit and regenerates it taking, as the starting point, the head of the branch you're currently on.
您可以认为樱桃选择类似于 rebase,或者更确切地说它像 rebase 一样管理。我的意思是,它需要一个现有的提交并重新生成它,以您当前所在的分支的头为起点。
A rebase
takes a commit that had a parent X and regenerates the commit as if it actually had a parent Y, and this is precisely what a cherry-pick
does.
Arebase
接受具有父 X 的提交并重新生成提交,就好像它实际上具有父 Y 一样,这正是 acherry-pick
所做的。
Cherry pick is more about how you select the commits. With pull
(rebase), git implicitly regenerates your local commits on top of what's pulled to your branch, but with cherry-pick
you explicitly choose some commit(s), and implicitly regenerate it (them) on top of your current branch.
Cherry Pick 更多的是关于你如何选择提交。使用pull
(rebase),git 在拉到分支的内容之上隐式地重新生成您的本地提交,但是cherry-pick
您明确地选择一些提交,并在您当前的分支之上隐式地重新生成它(它们)。
So the way you do it differs, but under the hood they are very similar operations - the regeneration of commits.
所以你做的方式不同,但在引擎盖下它们是非常相似的操作 - 提交的重新生成。
回答by Ajeet Sharma
It's kind of like Copy (from somewhere) and Paste (to somewhere), but for specific commits.
它有点像复制(从某处)和粘贴(到某处),但针对特定的提交。
If you want to do a hot fix, for example, then you can use the cherry-pick
feature.
例如,如果您想进行热修复,则可以使用该cherry-pick
功能。
Do your cherry-pick
in a development branch, and merge
that commit to a release branch. Likewise, do a cherry-pick
from a release branch to master. Voila
做你cherry-pick
在开发分支,merge
即承诺一个发布分支。同样,cherry-pick
从一个发布分支到 master 分支。瞧
回答by Wolfack
When you are working with a team of developers on a project, managing the changes between a number of git branches can become a complex task. Sometimes you don't want to merge a whole branch into another, and only need to pick one or two specific commits. This process is called 'cherry picking'.
当您与一组开发人员一起进行项目时,管理多个 git 分支之间的更改可能会成为一项复杂的任务。有时您不想将整个分支合并到另一个分支中,而只需要选择一两个特定的提交。这个过程被称为“樱桃采摘”。
Found a great article on cherry picking, check it out for in-depth details: https://www.previousnext.com.au/blog/intro-cherry-picking-git
找到了一篇关于樱桃采摘的好文章,查看详细信息:https: //www.previousnext.com.au/blog/intro-cherry-picking-git
回答by Bills
If you want to merge without commit ids you can use this command
如果你想在没有提交 ID 的情况下进行合并,你可以使用这个命令
git cherry-pick master~2 master~0
The above command will merge last three commits of master from 1 to 3
上面的命令将合并 master 从 1 到 3 的最后三个提交
If you want to do this for single commit just remove last option
如果您想为单次提交执行此操作,只需删除最后一个选项
git cherry-pick master~2
This way you will merge 3rd commit from the end of master.
通过这种方式,您将从 master 的末尾合并第 3 次提交。