Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Git Cherry Pick: How It Works & When to Use It

· 6 min read · Home Tech

In this article
  1. What Exactly Does Cherry-Pick Do?
  2. How to Use Git Cherry-Pick
  3. When Should You Cherry-Pick?
  4. Cherry-Pick vs. Merge vs. Rebase
  5. Common Problems and How to Fix Them
  6. Undoing a Cherry-Pick
  7. Best Practices and Pitfalls
  8. Frequently Asked Questions

Git cherry-pick copies a specific commit from one branch and applies it onto another, letting you bring over a single change without merging the whole branch. You use it when you need one fix or feature from a branch that isn’t ready to be merged, or when a commit landed on the wrong branch. This article explains the command, its options, common pitfalls, and when it’s better to use merge or rebase instead.

What Exactly Does Cherry-Pick Do?

Every commit in Git is a snapshot of the entire project at that moment, plus a reference to its parent commit. Cherry-pick takes the diff introduced by a commit (the changes between that commit and its parent) and replays it on top of your current branch. The result is a new commit with a different SHA-1 hash, because it has a different parent and timestamp, even though the content changes are identical.

For example, if you have a develop branch and a hotfix branch with a critical bug fix, you can cherry-pick just that fix onto main without bringing over any other hotfix commits. The original commit stays on hotfix, and a copy appears on main.

How to Use Git Cherry-Pick

The basic syntax is simple:

git cherry-pick <commit-hash>

You can specify a single commit, a range, or multiple commits. Common forms:

  • git cherry-pick a1b2c3d – apply the commit with hash a1b2c3d
  • git cherry-pick a1b2c3d e4f5a6b – apply several commits in the order given
  • git cherry-pick main..feature – apply all commits on feature that are not on main (the range syntax)
  • git cherry-pick -n a1b2c3d – apply the changes but don’t create a commit (stages them for you to commit manually)

Before cherry-picking, make sure your working tree is clean (commit or stash your changes). Then switch to the branch where you want the commit to land:

  1. git checkout main
  2. git cherry-pick a1b2c3d
  3. If there are no conflicts, Git creates the new commit automatically.
  4. If there are conflicts, resolve them, then run git cherry-pick --continue.

When Should You Cherry-Pick?

Cherry-pick is most useful in these scenarios:

  • Hotfix to production: A bug fix was made on a development branch, but you need it on the release branch immediately.
  • Accidental commit on the wrong branch: You committed to main but meant to commit to feature. Cherry-pick the commit onto feature, then reset main.
  • Selective porting: You have a long-lived branch with many changes, but only one specific change is needed elsewhere.
  • Collaboration workflows: A teammate made a commit on their branch and you want exactly that commit in your branch without merging their whole branch.

However, cherry-pick is not a substitute for merge or rebase in normal feature integration. It should be used sparingly because it creates duplicate commits, which can lead to confusion and conflicts when you later merge the original branch.

Cherry-Pick vs. Merge vs. Rebase

Understanding the difference helps you choose the right tool:

  • Merge: Combines two branches by creating a merge commit. It brings over all changes from the other branch and preserves the full history.
  • Rebase: Moves your branch’s commits on top of another branch, rewriting history. It makes the history linear but changes commit hashes.
  • Cherry-pick: Copies only the commits you specify. It does not merge branches or move your branch; it just replays selected commits.

In practice, use merge when you want to combine entire branches, rebase to keep a clean linear history, and cherry-pick only when you need a specific commit in isolation.

Common Problems and How to Fix Them

Cherry-pick can hit snags. Here are the most common issues and solutions:

  • Merge conflicts: If the same lines were changed differently in the target branch, Git will pause and mark conflicts. Resolve them in your editor, then run git add . and git cherry-pick --continue. To abort the whole operation, use git cherry-pick --abort.
  • Empty commit: If the changes are already present in the target branch (e.g., the commit was already cherry-picked), Git will say “The previous cherry-pick is now empty.” You can skip it with git cherry-pick --skip or force an empty commit with git cherry-pick --allow-empty.
  • Wrong commit hash: Make sure you copy the full hash or enough characters to be unique. Use git log --oneline to see recent commits.
  • Cherry-pick a merge commit: By default, Git refuses to cherry-pick a merge commit because it has two parents. You need to specify the mainline parent with -m 1 or -m 2 to tell Git which parent to compare against.

Undoing a Cherry-Pick

If you cherry-picked a commit and want to undo it, you have two main options:

  • Reset (if you haven’t pushed): git reset --hard HEAD~1 will remove the cherry-picked commit and any changes it brought.
  • Revert (if you have pushed): git revert <commit-hash> creates a new commit that undoes the changes, preserving history.

Be careful with reset --hard because it discards uncommitted changes. Always check git status first.

Best Practices and Pitfalls

Cherry-pick is powerful but can create duplicate commits that make history confusing. Follow these practices:

  • Keep a record: When you cherry-pick, add a note in the commit message referencing the original commit hash, e.g., (cherry-picked from a1b2c3d). This helps future readers trace the change.
  • Prefer merge or rebase for regular integration: If you find yourself cherry-picking many commits from the same branch, you’re probably better off merging or rebasing.
  • Watch out for dependencies: If the commit you cherry-pick depends on earlier commits from the same branch, those changes won’t be included. You may need to cherry-pick a range.
  • Test after cherry-picking: The new commit may not compile or pass tests if the surrounding code is different. Always run your test suite.

Frequently Asked Questions

What is the difference between cherry-pick and merge?

Merge brings over all commits from another branch and creates a merge commit that ties the histories together. Cherry-pick copies only the specific commits you name, creating new commits on the target branch without any branch relationship.

Can I cherry-pick multiple commits at once?

Yes. You can list multiple commit hashes, use a range like git cherry-pick A..B, or use the -n flag to stage all changes without committing them individually.

Why does cherry-pick create a new commit hash?

Because a commit’s hash is based on its contents, parent, timestamp, and message. Since the parent and timestamp differ, the new commit gets a new hash even if the changes are identical.

How do I cherry-pick a merge commit?

Use git cherry-pick -m 1 <merge-commit-hash> (or -m 2) to tell Git which parent to treat as the base. The -m option specifies the parent number.

What if the cherry-pick results in an empty commit?

That means the changes are already present in the target branch. You can skip it with git cherry-pick --skip or force an empty commit with --allow-empty if you need a placeholder.

Can I cherry-pick a commit from a branch that has been deleted?

Yes, as long as you still have the commit hash. Git keeps unreachable commits for a while, but they can be garbage-collected. If you have the hash, you can still cherry-pick it.