Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does cherry-picking a commit with Git mean?

What does git cherry-pick <commit> do?

like image 570
Rahul Avatar asked Feb 18 '12 07:02

Rahul


People also ask

How do you cherry pick one commit?

The easiest and most common way of using it is cherry-picking a single commit: git cherry-pick <COMMIT HASH> . git add . The commands above do the following: Create a new branch and switch to it.

How do you commit cherry pick changes?

To change the commit message when cherry-picking, use “git cherry-pick” with the “-e” option. As illustrated in this example, your default editor will open and it will let you change the commit message. When you are satisfied with the edits, save your file and your commit message should be saved successfully.

What happens when you cherry pick a merge commit?

This command is also beneficial for undoing changes related to the past development done by the team. For instance, if we accidentally made a commit to the wrong branch, we will shift to the correct branch and cherry-pick the commit to the right place where it should belong in a new branch of the repository.


3 Answers

Cherry picking in Git means to choose a commit from one branch and apply it onto another.

This is in contrast with other ways such as merge and rebase which normally apply many commits onto another branch.

  1. Make sure you are on the branch you want to apply the commit to.

     git switch master
    
  2. Execute the following:

     git cherry-pick <commit-hash>
    

N.B.:

  1. 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.

  2. 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>
    

Additional links:

  • git official guide page
like image 164
Philip Fourie Avatar answered Oct 06 '22 13:10

Philip Fourie


This quote is taken from: Version Control with Git

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 checkout rel_2.3
$ git cherry-pick dev~2 # commit F, below

before: before

after: after

Also, here is a very nice in action video tutorial about it: Youtube: Introduction to Git cherry-pick

like image 32
Teoman shipahi Avatar answered Oct 06 '22 13:10

Teoman shipahi


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.

To use it, you just need git cherry-pick hash, where hash is a commit hash from other branch.

For full procedure see: http://technosophos.com/2009/12/04/git-cherry-picking-move-small-code-patches-across-branches.html

like image 194
Tadeck Avatar answered Oct 06 '22 13:10

Tadeck