In collaborative development, sometimes you want to apply a specific commit from one branch to another — without merging the entire branch. That’s where git cherry-pick
comes in.
This guide will walk you through what cherry-picking is, when to use it, and how to safely apply it in real-world Git workflows.
🎯 What Is git cherry-pick
?
git cherry-pick
allows you to selectively apply individual commits from one branch onto another.
It’s perfect when:
- You want to backport a bug fix to a release branch.
- You need just one feature or change from a feature branch.
- You made a commit on the wrong branch and want to move it.
🔧 How to Use git cherry-pick
1. Get the Commit Hash
Use the following to view commit history:
git log --oneline
Find the SHA (commit hash) of the commit you want to cherry-pick. For example:
a1b2c3d Fix login issue
2. Checkout the Target Branch
Switch to the branch where you want to apply the commit:
git checkout main
3. Run Cherry-Pick
git cherry-pick a1b2c3d
This will apply that commit on top of the current branch.
🧰 Cherry-Picking Multiple Commits
To cherry-pick a range of commits:
git cherry-pick hash1^..hash2
Example:
git cherry-pick 1234abc^..5678def
This applies all commits from 1234abc
up to and including 5678def
.
⚠️ Resolving Conflicts
If Git encounters a conflict while cherry-picking:
- Git will pause and mark the files as conflicted.
- Resolve the conflicts manually.
- Run:
git add .
git cherry-pick --continue
To abort the cherry-pick:
git cherry-pick --abort
🧠 Summary of Commands
Task | Command |
---|---|
View commits | git log --oneline |
Cherry-pick single commit | git cherry-pick <commit-hash> |
Cherry-pick a range | git cherry-pick hash1^..hash2 |
Resolve and continue | git cherry-pick --continue |
Abort cherry-pick | git cherry-pick --abort |
🏁 Final Thoughts
git cherry-pick
is a powerful tool for surgical Git operations. Use it when you need just one commit from a branch without merging everything else.