← Back to DevBytes

Git Cherry-Pick

What Is Git Cherry-Pick?

git cherry-pick is a Git command that lets you take one or more specific commits from any branch, and apply them as new commits on top of your current branch. Instead of merging an entire branch, you select exactly the commits you need, and replay them wherever you are.

Think of it as "picking a cherry" from the commit tree—you pluck a single commit (or a few) and place it onto another branch, preserving the author, timestamp, and message (unless you choose to modify them). The commit will have a new SHA-1 hash because it becomes a new commit object with a different parent, but the changes introduced are identical.

Why Cherry-Pick Matters

🚀 Deploy your AI agent in 10 minutes

Managed Hermes hosting. Zero DevOps. 100M tokens/mo included.

Try it free →

Cherry-picking is a powerful tool for selective integration. It solves several common development workflow problems:

Using cherry-pick avoids the "all or nothing" nature of merges, giving you fine-grained control over what goes into your branch.

How to Use Git Cherry-Pick

Cherry-Picking a Single Commit

The simplest form picks one commit by its hash:

git cherry-pick <commit-hash>

For example, suppose you have two branches: main and feature. The feature branch has a commit a1b2c3d that you want on main. First, switch to main:

git checkout main
git cherry-pick a1b2c3d

Git will apply the changes from a1b2c3d and create a new commit. You'll see output like:

[main 5e6f7a8] Fix login validation error
 Date: Mon Apr 7 10:30:00 2025 +0200
 1 file changed, 5 insertions(+), 2 deletions(-)

The new commit on main has a different hash but the same commit message (unless you edit it).

Cherry-Picking Multiple Commits

You can cherry-pick a range of commits or a list of individual hashes:

# Pick a linear range (inclusive) from commit A to commit B, where A is older
git cherry-pick A..B

# Or specify multiple separate commits
git cherry-pick hash1 hash2 hash3

The range syntax A..B includes commits reachable from B but not from A. Typically you want the commits between two points on a branch. For example, to bring the last three commits from feature into main:

git checkout main
# Assuming the feature branch has commits: d4e5f6a, 7b8c9d0, e1f2g3h (oldest to newest)
git cherry-pick d4e5f6a..e1f2g3h

Git will apply each commit in order. If any commit fails due to conflicts, the whole process pauses and waits for you to resolve the conflict.

Handling Conflicts During Cherry-Pick

Conflicts happen when the changes in the cherry-picked commit clash with the current state of your branch. Git will pause mid-process and mark the conflicted files. You'll see:

error: could not apply a1b2c3d... Fix login validation
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

To resolve:

  1. Edit the conflicted files and remove the conflict markers (<<<<<<, =======, >>>>>>).
  2. Stage the resolved files: git add <file>.
  3. Continue the cherry-pick: git cherry-pick --continue.

If you want to abort the entire cherry-pick and go back to the pre-cherry-pick state, use:

git cherry-pick --abort

To skip a commit that can’t be applied and move on to the next (only valid when picking multiple commits), use:

git cherry-pick --skip

Useful Options

Cherry-pick comes with several options that tailor its behavior:

Best Practices and Pitfalls

When to Use Cherry-Pick

Cherry-pick shines in targeted, low-risk situations:

When to Avoid Cherry-Pick

Overusing cherry-pick can lead to a tangled history and duplicate commits (same change, different hashes) that make future merges confusing. Avoid cherry-pick when:

Keeping History Clean

Cherry-picking creates a new commit with a different hash. If you later merge the branch that originally contained the commit, Git will see the same change introduced twice. Usually Git’s merge logic can detect duplicate patches and skip them, but it’s not foolproof. To minimize confusion:

Cherry-Pick and Branching Workflows

In Gitflow or similar workflows, cherry-pick is often the recommended way to bring hotfixes from main (or a dedicated hotfix branch) back into develop. For example:

# On the hotfix branch, commit the fix
git checkout hotfix/login-fix
# ... fix bug, commit as a1b2c3d
git checkout develop
git cherry-pick -x a1b2c3d
git checkout main
git cherry-pick -x a1b2c3d

This keeps the hotfix isolated and applies it exactly where needed without merging the entire hotfix branch prematurely.

Testing Cherry-Picks

Always test the result of a cherry-pick. The applied changes may interact differently with the target branch’s codebase. Run your test suite and consider doing the cherry-pick on a temporary branch first:

git checkout -b test-cherry-pick main
git cherry-pick a1b2c3d
# Run tests, if all good:
git checkout main
git cherry-pick a1b2c3d
git branch -D test-cherry-pick

Automating Cherry-Pick in Scripts

Cherry-pick can be scripted for repetitive tasks. A common pattern is to cherry-pick all commits from a specific author or with a certain keyword in the message. For example, using git log and a loop:

# Pick all commits by author "Jane" from feature-branch into main
git checkout main
commits=$(git log feature-branch --author="Jane" --format="%H" --reverse)
for hash in $commits; do
  git cherry-pick $hash
done

Be careful: if any cherry-pick fails, the script may need to handle the conflict or abort gracefully. A production script would check exit codes and use --continue or --abort as needed.

Conclusion

git cherry-pick is an essential precision tool in any developer's Git toolbox. It empowers you to surgically move commits across branches, enabling hotfix propagation, commit recovery, and selective feature integration. By understanding its basic usage, conflict resolution, and the rich set of options like -x and -n, you can handle complex version control scenarios with confidence.

However, with great power comes responsibility. Overuse can muddy your project history and create duplicate commits that confuse merges. Stick to cherry-picking isolated, self-contained changes, always record the original commit hash when possible, and test the results thoroughly. When you need to integrate a whole feature or a chain of dependent commits, favor merging or rebasing instead. Used wisely, cherry-pick keeps your branches lean and your history meaningful.

🚀 Need a reliable AI agent for your project?

Deploy Hermes Agent in 10 minutes. Managed hosting, zero DevOps.

Get Started — $23.99/mo
← Back to all articles