• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • AI
  • Javascript
  • TypeScript
  • Development
  • Frameworks
    • Angular
    • Git
    • NestJs

The code Mood

Ignite Passion, Master Code

You are here: Home / Frameworks / Git / Git Move Commit to Another Branch: 5 Essential Steps for an Easy Fix

Git Move Commit to Another Branch: 5 Essential Steps for an Easy Fix

by Ahmed Fakhar Abbas

Git move commit to another branch — if you’ve been coding long enough, you’ve either searched this phrase at 2 AM or cursed yourself for forgetting it. We’ve all been there: you’re on the wrong branch, you commit, and boom — your shiny new code is sitting where it doesn’t belong.

Good news? Git gives us ways to fix this without blowing up the repo. The process isn’t scary once you’ve done it once. In this guide, I’ll walk through the exact steps I use whenever I drop a commit in the wrong place. And I’ll sprinkle in some personal notes and gotchas that Git docs won’t tell you.

Table of Contents

Toggle
  • The Scenario: Oops, Wrong Branch Again
  • Step 1: Identify the Commit to Move
  • Step 2: Move to the Right Branch
  • Step 3: Git Move Commit to Another Branch (Cherry-Pick Method)
  • Step 4: Clean Up the Source Branch
  • Bonus: Safer Workflows to Avoid This Mess
  • Why You Might Need to Git Move Commit to Another Branch
  • Alternative Approaches
    • Interactive Rebase
    • Format-Patch + Apply
  • Conclusion

The Scenario: Oops, Wrong Branch Again

Imagine this: you’re working on a feature branch (feature-login). You add a commit — let’s call it a1b2c3d. Then, five minutes later, you realize… wait. This should have gone into feature-auth.

If you’ve not pushed to remote yet, it’s an easy fix. If you have, it’s still possible — but then we’re talking force pushes and potential conflict with teammates (⚠️ not fun). For now, let’s focus on the local scenario.

Step 1: Identify the Commit to Move

First, figure out which commit you need to move. Run:

git log --oneline

You’ll see something like:

a1b2c3d  Added login validation
9f8e7d6  Fixed typo in README

Take note of that commit hash (a1b2c3d). You’ll need it in the next step.

💡 Note: Copy just enough characters (like a1b2c3d) — Git is fine with shortened hashes as long as they’re unique.

Step 2: Move to the Right Branch

If the target branch (feature-auth) already exists, just switch:

git checkout feature-auth

If not, create and switch in one go:

git checkout -b feature-auth

⚡ Pro Tip: I often start from main or develop before branching, so I know I’m branching off the right place.

Step 3: Git Move Commit to Another Branch (Cherry-Pick Method)

Now you’re on the correct branch. This is where the magic happens. Use git cherry-pick:

git cherry-pick a1b2c3d

That copies the commit over. Git will replay it on your current branch.

If all goes well, you’ll see something like:

[feature-auth 1234567] Added login validation
 1 file changed, 10 insertions(+), 0 deletions(-)

🎉 Done. Your commit now lives on the branch it should have been on.

Step 4: Clean Up the Source Branch

Now go back to the branch where you made the mistake (feature-login):

git checkout feature-login

Then reset it to the commit just before your misplaced one:

git reset --hard <previous_commit_hash>

Replace <previous_commit_hash> with the hash before a1b2c3d.

⚠️ Warning: This erases history on that branch. If you already pushed it, don’t use --hard reset unless you’re ready to deal with force pushes.

Bonus: Safer Workflows to Avoid This Mess

Look, mistakes happen. But here are a few things that save me from doing “git move commit to another branch” searches every other week:

  • Always git status before committing. It’s like checking the room before leaving.
  • Use meaningful branch names (don’t just call it test or stuff).
  • Consider enabling Git worktrees if you often juggle multiple branches.
  • Set up Git hooks that warn you when you’re about to commit on main.

💡 Note: If you’re curious, I wrote about renaming a Git branch — another small thing that often pairs with moving commits.

Why You Might Need to Git Move Commit to Another Branch

Besides the classic “wrong branch” mistake, here are other cases where this trick saves the day:

  • You started coding a feature, but realized it belongs in a bugfix branch.
  • You committed to main out of habit (we’ve all done it).
  • You’re experimenting and want to fork work into a separate branch mid-way.

This isn’t just about fixing mistakes — it’s about keeping history clean and logical.

Alternative Approaches

Cherry-pick is the go-to, but here are two more moves in your Git toolbox:

Interactive Rebase

If the commit is recent, you can use:

git rebase -i HEAD~3

Then move or drop commits as needed. A bit advanced, but super flexible.

Format-Patch + Apply

For patch lovers:

git format-patch -1 a1b2c3d
git checkout feature-auth
git apply 0001-Added-login-validation.patch

I rarely use this, but it’s nice to know it exists.

Conclusion

So there you go. When you need to git move commit to another branch, cherry-pick is your best friend. It’s quick, reliable, and keeps your history clean.

If you ever commit to the wrong branch again (spoiler: you will), don’t sweat it. Run through these steps, reset the source branch, and you’re back on track.

Happy coding — and may your commits always land in the right place!

Filed Under: Git

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Recent Posts

  • 5 Programming Jokes That Prove Java Developers Have the Best Sense of Humor
  • Bridging the Gap: The Crucial Role of Developer Advocates in the Software Landscape
  • Long Hair and Beard: 9 Fascinating Secrets Behind Programmers’ Iconic Look
  • ServiceNow vs Salesforce: 7 Must-Know Differences to Choose the Best CRM Solution
  • Will Devin AI Take Your Job?

Categories

  • AI
  • Angular
  • Development
  • Git
  • Javascript
  • NestJs
  • TypeScript

Footer

  • About Us
  • Privacy Policy
  • Contact Us

Copyright © 2025 · The code Mood