Can anyone explain what git cherry-pick <sha> does?

一笑奈何 提交于 2019-12-01 03:32:55

问题


As my concern here is, I have old commit in my another local branch [contains abc.cpp, def.cpp].

Now after few months I want use those changes, but in my current branch abc.cpp is upgraded. So is it like if I cherry pick then it will integrate changes of old abc.cpp into new abc.cpp [recent working directory copy]?


回答1:


The git-cherry-pick(1) man page says:

Given one or more existing commits, apply the change each one introduces, recording a new commit for each. This requires your working tree to be clean (no modifications from the HEAD commit).

In plain English, this means that git cherry-pick applies commits from one branch to another, but does not preserve the original history or ancestry from the other branch in the way that a proper merge would do.

Think of it as applying a series of selected patches, rather than a full merge of two branches of history. Obviously, if you tend to make very small, atomic commits then cherry-picking looks exactly like applying a well-written patch. However, since you don't have common ancestors the way you do with merge or rebase, you may have a lot more conflicts to resolve if your commits aren't small and isolated.

Whether or not cherry-picking is a good idea is highly dependent on how you structure your commits. If it doesn't work for you, you can always do things more manually with git format-patch and git apply instead.




回答2:


Yes, that's what it does. cherry-pick is applying a commit (or a range of them) as a patch to your branch (well, almost as a patch).

You might have conflicts (like when you merge branches) since independent modifications have happened on your branches.




回答3:


Note that with git1.8.5/1.9 (Q4 2013), git cherry-pick can now easily cherry-pick "from the previous branch":

Just like "git checkout -" knows to check out and "git merge -" knows to merge the branch you were previously on, "git cherry-pick" now understands "git cherry-pick -" to pick from the previous branch.

See commit 182d7d from Hiroshige Umino (yaotti):

cherry-pick: allow "-" as abbreviation of '@{-1}'

"-" abbreviation is handy for "cherry-pick" like "checkout" and "merge".

It's also good for uniformity that a "-" stands as the name of the previous branch where a branch name is accepted and it could not mean any other things like stdin.



来源:https://stackoverflow.com/questions/11243683/can-anyone-explain-what-git-cherry-pick-sha-does

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!