Back to Blog
git

How to Merge Only Specific Changes from a Git Branch: Avoiding Unwanted Commits

Jomar Montuya
July 29, 2025
3 minutes read

How to Merge Only Specific Changes from a Git Branch

Quick Answer / TL;DR If you just want to copy one specific commit (e.g., 9c4e7d2) from a feature branch to master:

  1. Switch to master: git checkout master
  2. Find the commit hash: git log feature-branch
  3. 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.


3 Steps to Cherry-Pick Like a Pro

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.

Step 1: Find the Commit Hash

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.

Step 2: Switch and Pick

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

Step 3: Verify

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.


Troubleshooting Common Issues

What if I get a conflict?

Conflicts happen if the file you're fixing has changed differently on master.

  1. git status to see the conflicted files.
  2. Open the files and resolve the code (remove <<<<<<< markers).
  3. git add <file>
  4. git cherry-pick --continue

What if I picked the wrong commit?

If you haven't pushed yet, you can easily undo the last cherry-pick:

$ git reset --hard HEAD~1

Can I copy a whole range of commits?

Yes. If you want everything between commit A and commit B:

$ git cherry-pick A..B

Alternative Method: When Cherry-Pick Fails

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.

About Jomar Montuya

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.

Expertise:

Philippine Software DevelopmentConstruction TechEnterprise AutomationRemote Team BuildingNext.js & ReactFull-Stack Development

Let's Build Something Great Together!

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