Git & GitHub ยท Chapter 37 of 42

git cherry-pick

`git cherry-pick` applies a specific commit from one branch onto another, without merging the entire branch. It's useful when you need just one fix or feature from another branch.

Cherry-picking creates a new commit with the same changes but a different hash, since it's technically a new commit on the target branch.

Syntax
git cherry-pick <commit-hash>

Picking a single commit

Run `git cherry-pick commit-hash` while on the target branch to apply just that one commit's changes on top of your current branch.

Handling conflicts

If the cherry-picked commit conflicts with your current branch, Git pauses so you can resolve conflicts manually, then continue with `git cherry-pick --continue`.

Example 1 (bash)
git switch main
git cherry-pick 9c2e1aa
Output
[main 3e8f1cc] Fix crash when cart is empty
 Date: ...

Applies the single commit 9c2e1aa from another branch onto main as a new commit.

Example 2 (bash)
git cherry-pick --continue
Output
[main 4a9d2bb] Fix crash when cart is empty

Continues a cherry-pick after manually resolving a conflict caused by the picked commit.

Key points

  • git cherry-pick applies one specific commit onto another branch.
  • It creates a new commit with a different hash on the target branch.
  • Conflicts during cherry-pick must be resolved manually.
  • It's useful for backporting a single fix without merging a whole branch.
๐Ÿ’ก Note: Cherry-picking is handy for hotfixes that need to go to both a release branch and main.

๐Ÿ“ Quick Quiz

1. What does git cherry-pick do?

2. Does the cherry-picked commit keep the same hash?

3. How do you continue a cherry-pick after resolving a conflict?