Transferred from: http://hubingforever.blog.163.com/blog/static/1710405792012587115533/
This document is compiled from:
http://sg552.iteye.com/blog/1300713 Http://web.mit.edu/bitbucket/git-doc/git-cherry-pick.txtgit Cherry-pickused to apply a commit modification of another local branch to the current branch.
Practical Issueson a localMasteron the branch made aCommit (38361a68138140827b31b72f8bbfd88b3705d77a), how do I put it on a local OLD_CC branch? One way: UseCherry-pick. According to the GIT documentation:Apply The changes introduced by some existing commitsis to apply the existing commit (which is understood to be resubmitted)
Simple Usage: Git cherry-pick <commit id> for example:$ git checkout old_cc$ gitCherry-pick38361a681.If it goes well, it will be submitted normally. Results:finished one cherry-pick.# on Branch old_cc# Your Branch is ahead of ' ORIGIN/OLD_CC ' by 3 commits.2. If there is a conflict in the process of Cherry-pickAutomatic Cherry-pick failed. After resolving the conflicts,mark the corrected paths with ' git add <paths> ' or ' git rm <paths> 'and commit the result with: git commit-c 15a2b6c61927e5aed6718de89ad9dafba939a90bAs with ordinary conflicts, manual resolution:execute git status to see which files conflict$ git statusboth modified:app/models/user.rbThen manually resolve the conflicting files, then change to add to the index via git add, and finally execute the GIT commit commit modification. $ vim app/models/user.rb$ git add app/models/user.rbgit commit-c <Original Commit number>Git-cherry-pick (1) ================== name----git-cherry-pick-apply The changes introduced by some existing Commits synopsis--------' git cherry-pick ' [--edit] [-n] [-M parent-number] [-S] [-X] [--ff] <commit> .... description----------- given One or more existing commits, apply the change for each oneintroduces, recording a new Commit for each. this requires yourworking tree to being clean (no modifications from the HEAD commit). when It's isn't obvious how To apply a change, the followinghappens: 1. The current branch and ' HEAD ' pointer stay at the last commit successfully made.2. The ' Cherry_pick_head ' ref is set-to-point at the commit that introduced the change, that's difficult to apply .3. Paths in which the change applied cleanly is updated both in the index file and in your working tree.4. For conflicting paths, the index file records up to three versions, as described in the ' TRUE MERGE ' section O F &NBSP;LINKGIT:GIT-MERGE[1]. the working tree files would include a description of the conflict bracketed by the usual con Flict markers ' <<<<<<< ' and ' >>>>>>> '. 5. No other modifications is Made. see linkgit:git-merge[1] for some hints on resolving suchconflicts. options--- ----<commit>..::commits to Cherry-pick.for a more complete list of ways to spell commits, seelinkgit:gitrevisions[7].sets of commits can be passed and no traversal is do bydefault, as if the '--no-walk ' option is specified, seelinkgit:git-rev-list[1].-e::--edit::with this option, ' git cherry-pick ' would let you edit the commitmessage prior to committing.-X::When recording the commit, append to the original commitmessage A note that indicates which commit the changeWas cherry-picked from. Append the note only for Cherrypicks without conflicts. do not use the This option ifYou is cherry-picking from your private branch becauseThe information is useless to the recipient. If on theOther hand cherry-picking between, publiclyvisible branches (e.g. backporting a fix to aMaintenance Branch for the older release from aDevelopment Branch), adding this information can beuseful.-R::It used to be, the command defaulted to do '-X 'described above, and '-R ' is to disable it. now theThe default is not to do '-X ' so this option is a no-op.-M parent-number::--mainline parent-number::usually you cannot cherry-pick a merge because you does not know whichside of the merge should be considered the mainline. thisoption specifies the parent number (starting from 1) ofThe mainline and allows Cherry-pick to replay the changerelative to the specified parent.-n::--no-commit::usually the command automatically creates a sequence of commits.This flag applies the changes necessary to Cherry-pickEach named commits to your working tree and the index,without making any commit. In additionoption is used, your index does not has the to match theHEAD commit. The Cherry-pick is done against thebeginning state of your index.+this is useful if cherry-picking more than one commits ' effect to your index in a row. -s::--signoff::Add signed-off-by Line at the end of the commit message.--FF::If The current HEAD is the same as the parent of theCherry-pick ' Ed commit, then a fast forward to this commit wouldBe performed.--strategy=<strategy>::Use the given merge strategy. Should only be used once.See the MERGE strategies sections in linkgit:git-merge[1]For details.-x<option>::--strategy-option=<option>::Pass the merge strategy-specific option through to themerge Strategy. See linkgit:git-merge[1] for details.EXAMPLES--------git cherry-pick master::Apply The change introduced by the commit at the tip of theMaster Branch and create a new commit with the this change.Git Cherry-pick. Master::git Cherry-pick ^head Master::Apply The changes introduced by all commits that is ancestorsof master but not of the HEAD to produce new commits.Git cherry-pick master{tilde}4 master{tilde}2::Apply The changes introduced by the fifth and thirdcommits pointed to by master and create 2 new commits withthese changes.Git cherry-pick-n master~1 Next::Apply to the working tree and the index the changes introducedBy the second last commit pointed to by master and by the lastcommit pointed to is next, but does not create an any commit withthese changes.Git Cherry-pick--ff. Next::If history is linear and HEAD are an ancestor of next, updateThe working tree and advance the HEAD pointer to match next.Otherwise, apply the changes introduced by those commitsis in next and not HEAD to the current branch, creating a newcommit for each new change.git rev-list--reverse master \--README | Git cherry-pick-n--stdin::Apply The changes introduced by all commits on the masterbranch that touched README to the working tree and Index,The result can be inspected and made to a single newcommit if suitable. the following sequence attempts to backport a patch, bails out becausethe code The patch applies to have changed too Much, and then triesagain, this time exercising more care about matching up context lines. ------------$ git cherry-p Ick topic^ <1>$ git diff <2>$ git reset--merge orig_head <3>$ git Cherry-pick-xpatience topic^ <4>------------<1> Apply the change this would be shown by ' Git show Topi c^ '. In this example, the patch does isn't apply cleanly, soinformation about the conflict are written to the index andworking tre E and no new commit results.<2> summarize changes to be reconciled<3> cancel the Cherry-pick. in other words, return to Thepre-cherry-pick state, preserving any local modifications you had inthe working TREE.&L T;4> try to apply the change introduced by ' topic^ ' again,spending extra time-to-avoid mistakes based on incorrectly matchingcontext lines.
Git cherry-pick Introduction (reprint)