Quick Answer / TL;DR If you just want to copy one specific commit (e.g.,
9c4e7d2) from a feature branch to master:
- Switch to master:
git checkout master- Find the commit hash:
git log feature-branch- Copy it over:
git cherry-pick 9c4e7d2
Every developer has faced this nightmare: you create a fix branch off develop to patch a critical bug. But develop also contains 15 broken experimental features. If you run a standard git merge, you bring the bug fix and the broken features into production.
The solution is Git Cherry-Pick.
Think of cherry-pick as a surgical tool. Instead of merging a whole branch history, it allows you to copy-paste specific commits onto your clean branch.
Let's assume you have a branch fix-1 with a critical bug fix, but it’s mixed in with experimental code you don't want.
First, look at the history of your feature branch to find the ID (hash) of the commit you want to keep.
$ git log fix-1 --oneline --graph * 8f3a2b1 (fix-1) Fix: Handle null values in user preferences <-- WANT THIS * 9c4e7d2 Fix: Add validation for email format <-- WANT THIS * a1b2c3d (develop) Experimental: New dashboard layout <-- DON'T WANT * ...
In this case, we want 9c4e7d2 and 8f3a2b1.
Go to the branch where you want the changes (usually master or main) and run the cherry-pick command.
# 1. Switch to clean branch $ git checkout master # 2. Pick the commits (Oldest to Newest) $ git cherry-pick 9c4e7d2 $ git cherry-pick 8f3a2b1
Tip: You can pick multiple commits at once!
git cherry-pick 9c4e7d2 8f3a2b1
Always double-check that you only brought over what you intended.
$ git log --oneline -3 3d4e5f6 Fix: Handle null values in user preferences 7a8b9c2 Fix: Add validation for email format abc1234 Previous stable commit
If you see your fixes without the "Experimental" commits, you're safe to push.
Conflicts happen if the file you're fixing has changed differently on master.
git status to see the conflicted files.<<<<<<< markers).git add <file>git cherry-pick --continueIf you haven't pushed yet, you can easily undo the last cherry-pick:
$ git reset --hard HEAD~1
Yes. If you want everything between commit A and commit B:
$ git cherry-pick A..B
Sometimes commits are too messy or interdependent for cherry-picking. In those cases, you can use the Diff/Patch method to just copy the file changes without the commit history.
# 1. Create a patch file of changes between develop and your branch $ git diff develop..fix-1 > my-fix.patch # 2. Switch to master and apply it $ git checkout master $ git apply my-fix.patch
This applies the changes to your working files (unstaged). You can then test them and commit them as a fresh new commit.
This post was written by Jomar, Senior Developer at Medianeth. We help teams untangle complex Git workflows. Need help? Get in touch.
Founder & Lead Developer
With 8+ years building software from the Philippines, Jomar has served 50+ US, Australian, and UK clients. He specializes in construction SaaS, enterprise automation, and helping Western companies build high-performing Philippine development teams.
Ready to make your online presence shine? I'd love to chat about your project and how we can bring your ideas to life.
Free Consultation