http://think-like-a-git.net/sections/rebase-from-the-ground-up/cherry-picking-explained.html
Git's own online help has a great, if characteristically terse, description of what the command does:
Given one or more existing commits, apply the change each one introduces, recording a new commit for each.
I've already mentioned (back on the page about garbage collection) that a Git commit's ID is a hash of both its contents and its history. So, even if you have two commits that introduce the exact same change, if they point to different parent commits, they'll have different IDs.
What git cherry-pick
does, basically, is take a commit from somewhere else, and "play it back" wherever you are right now. Because this introduces the same change with a different parent, Git builds a new commit with a different ID.
Let's go back to this example from the reachability section:
If you were at node H in this graph, and you typed git cherry-pick E
(yes, you'd actually type part or all of the SHA for the commit, but for simplicity's sake, I'll just use the labels that are already here), you'd wind up with a copy of commit E—let's call it "E prime" or E'—that pointed to H as its parent, like so:
Or, if you typed something like git cherry-pick C D E
, you'd wind up with this when you were done:
The important thing to notice here is that Git has copied changes made in one place, and replayed them somewhere else.
Here's a quick slideshow that steps through the process:
原文有一个展示的系列图,想看的,可以去看原文
通过tortoisegit来操作cherry-pick
http://*.com/questions/9415534/cherry-pick-using-tortoisegit
Cherry Pick可能遇到的问题
http://*.com/questions/9229301/git-cherry-pick-says-38c74d-is-a-merge-but-no-m-option-was-given
如果操作的commit是一次合并记录的话,此次的commit是有2个父节点的,需要用户指明,cherry-pick哪一个父节点。这样做,会丢失掉这次合并的记录。
我的处理方法是,直接忽略这次的commit,不进行cherrypick
The way a cherry-pick works is by taking the diff a changeset represents (the difference between the working tree at that point and the working tree of its parent), and applying it to your current branch.
So, if a commit has two or more parents, it also represents two or more diffs - which one should be applied?
You're trying to cherry pick fd9f578
, which was a merge with two parents. So you need to tell the cherry-pick command which one against which the diff should be calculated, by using the -m
option. For example, git cherry-pick -m 1 fd9f578
to use parent 1 as the base.
I can't say for sure for your particular situation, but using git merge
instead of git cherry-pick
is generally advisable. When you cherry-pick a merge commit, it collapses all the changes made in the parent you didn't specify to -m
into that one commit. You lose all their history, and glom together all their diffs. Your call.
cherry pick遇到冲突的时候【使用tortoisegit】
tortoisegit正在进行的cherry-pick会停止下来,需要手动处理冲突文件,然后标记为已解决。记得要自己填写commit message,记录下是怎么处理冲突的。然后再点击commit
之后cherry pick会继续执行
不要经常使用cherry-pick
===2015年09月26日更新===
坑爹了,之前遇到的问题,cherry pick的commit如果是个合并记录的话,现在不想跳过
http://*.com/questions/9229301/git-cherry-pick-says-38c74d-is-a-merge-but-no-m-option-was-given
The way a cherry-pick works is by taking the diff a changeset represents (the difference between the working tree at that point and the working tree of its parent), and applying it to your current branch.
So, if a commit has two or more parents, it also represents two or more diffs - which one should be applied?
You're trying to cherry pick fd9f578
, which was a merge with two parents. So you need to tell the cherry-pick command which one against which the diff should be calculated, by using the -m
option.
For example, git cherry-pick -m 1 fd9f578
to use parent 1 as the base.
I can't say for sure for your particular situation, but using git merge
instead of git cherry-pick
is generally advisable.
When you cherry-pick a merge commit, it collapses all the changes made in the parent you didn't specify to -m
into that one commit.
You lose all their history, and glom together all their diffs. Your call.
会出现提示:
$ git cherry-pick -m 2 fa14668b19899a92b5a4db254af90b730d4bf4ba
On branch chucklu_master
Your branch and 'chucklu/master' have diverged,
and have 19 and 70 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
You are currently cherry-picking commit fa14668.
nothing to commit, working directory clean
The previous cherry-pick is now empty, possibly due to conflict resolution.
If you wish to commit it anyway, use:
git commit --allow-empty
Otherwise, please use 'git reset'
上面的分析,因为cherrypick的结果,没有任何改变,是没有意义的
http://*.com/questions/14096244/why-is-git-cherrypick-saying-nothing-to-commit
It's exactly what it says: the changes you're trying to cherry-pick are already wholly contained in the branch you're on. I.e. the result of the cherry-pick is no changes. You can create an empty commit with the --allow-empty
flag to indicate that you attempted to cherry-pick, but there were no changes to pull in.