Course Tonight

Course Tonight

Did You Know?

You can create any type of product documentation with Docy

Chapter 17: Cherry Picking

Estimated reading: 3 minutes 5 views

Parameters:

  • -e, --edit: With this option, git cherry-pick will let you edit the commit message prior to committing. When recording the commit, append a line that says “(cherry picked from commit …​)” to the original commit message to indicate which commit this change was cherry-picked from. This is done only for cherry picks without conflicts.
  • -x: If the current HEAD is the same as the parent of the cherry-picked commit, then a fast forward to this commit will be performed.
  • --ff: Continue the operation in progress using the information in .git/sequencer. Can be used to continue after resolving conflicts in a failed cherry-pick or revert.
  • --continue: Forget about the current operation in progress. Can be used to clear the sequencer state after a failed cherry-pick or revert.
  • --quit: Cancel the operation and return to the pre-sequence state.
  • --abort: Cancel the operation and return to the pre-sequence state.

A cherry-pick takes the patch that was introduced in a commit and tries to reapply it on the branch you’re currently on.

Source: Git SCM Book

Section 17.1: Copying a Commit from One Branch to Another

git cherry-pick <commit-hash> will apply the changes made in an existing commit to another branch while recording a new commit. Essentially, you can copy commits from one branch to another.

Given the following tree:

dd2e86 - 946992 - 9143a9 - a6fd86 - 5a6057 [master]
         \
          76cada - 62ecb3 - b886a0 [feature]

Let’s say we want to copy b886a0 to master (on top of 5a6057).

We can run:

git checkout master
git cherry-pick b886a0

Now our tree will look something like:

dd2e86 - 946992 - 9143a9 - a6fd86 - 5a6057 - a66b23 [master]
         \
          76cada - 62ecb3 - b886a0 [feature]

The new commit a66b23 has the same content (source diff, commit message) as b886a0, but with a different parent. Note that cherry-picking will only pick up changes from that specific commit (b886a0 in this case) and not all the changes in the feature branch (for that, you would need to use either rebasing or merging).

Section 17.2: Copying a Range of Commits from One Branch to Another

>

  • git cherry-pick <commit-A>..<commit-B> will place every commit after A and up to and including B on top of the currently checked-out branch.
  • git cherry-pick <commit-A>^..<commit-B> will place commit A and every commit up to and including B on top of the currently checked-out branch.
  • Section 17.3: Checking if a Cherry-pick is Required

    Before starting the cherry-pick process, you can check if the commit you want to cherry-pick already exists in the target branch. If it does, you don’t have to do anything.

    • git branch --contains <commit> lists local branches that contain the specified commit.
    • git branch -r --contains <commit> also includes remote tracking branches in the list.

    Leave a Comment

    Share this Doc

    Chapter 17: Cherry Picking

    Or copy link

    CONTENTS