git cherry-pick 從其他分支檢出指定的commit到當前分支


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://stackoverflow.com/questions/9415534/cherry-pick-using-tortoisegit

 

 

 

Cherry Pick可能遇到的問題

http://stackoverflow.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

http://dan.bravender.net/2011/10/20/Why_cherry-picking_should_not_be_part_of_a_normal_git_workflow.html

 

 

===2015年09月26日更新===

坑爹了,之前遇到的問題,cherry pick的commit如果是個合並記錄的話,現在不想跳過

 http://stackoverflow.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://stackoverflow.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.

 

 

 

cherry pick -m

-m parent-number--mainline parent-number

Usually you cannot cherry-pick a merge because you do not know which side of the merge should be considered the mainline.

This option specifies the parent number (starting from 1) of the mainline and allows cherry-pick to replay the change relative to the specified parent.

 

cherry-pick always use theirs when encounter conflicts

https://stackoverflow.com/questions/14337945/how-do-i-resolve-cherry-pick-conflicts-using-their-changes

First you should undo your cherry-pick, try to run this

git cherry-pick --abort

Second, try to make cherry-pick, but in this time you get their changes not yours, so make this:

git cherry-pick --strategy=recursive -X theirs {Imported_Commit}

 

https://stackoverflow.com/questions/45604767/git-cherry-picking-with-ours-theirs-strategy

The git cherry-pick command does have the --strategy and --strategy-option=<option> options.

They are passed through to the merge strategies.

So, in your case:

git cherry-pick --strategy-option=ours HASH1 HASH2 HASH3 -n

 

https://stackoverflow.com/questions/21051850/force-git-to-accept-cherry-picks-changes/32468531#32468531

you can tell it to always prefer the changes of the commit you are cherry-picking:

git cherry-pick commitish --strategy-option theirs   這個經過測試,是工作的 

commitish can be a SHA-1 hash of a commit, or a branch-name for the lastest commit of that branch, branch-name~1 for the commit before that etc.

If you want to do the reverse, use:

git cherry-pick commitish --strategy-option ours

The shorthand for --strategy-option is -X (uppercase X).

遇到處理完之后,還會沖突的情況,HEAD是delete,cherry-pick HEAD是modified,因為兩個操作不一致導致的。

 



cherry-pick a range of commits

https://stackoverflow.com/questions/46109211/cherry-picking-few-commits-from-another-branch

Let's say the history is A-B-C-D-E-F-G, and you'd like to cherry-pick C-D-E-F.

git cherry-pick B..F

or

git cherry-pick C^..F

or

git cherry-pick C D E F


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM