• 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 / How to Move a Commit to Another Branch in Git: A Quick and Easy Guide

How to Move a Commit to Another Branch in Git: A Quick and Easy Guide

by Ahmed Fakhar Abbas

Git is a powerful version control system that allows developers to manage their code efficiently. However, even experienced Git users can make mistakes, such as committing code to the wrong branch. In this article, we’ll explore a common scenario where you’ve made a commit on the wrong branch and guide you through the process of moving that commit to the correct branch. You can read about user configurations if you are facing any other issues related to commit, push, and pull.

Table of Contents

Toggle
  • The Scenario:
    • Step 1: Identify the Commit to Move
    • Step 2: Checkout the Master Branch (Optional)
    • Step 3: Create and Checkout the Target Branch (Optional)
    • Step 4: Cherry-Pick the Commit
    • Step 5: Reset the Source Branch
  • Conclusion:

The Scenario:

Picture this: you’ve just made a commit (let’s call it a1b2c3d) on a branch named feature-a, only to realize that it should have been on a different branch, say feature-b. Don’t panic! As long as you haven’t pushed these changes to the remote repository, there’s a straightforward solution to this problem.

Step 1: Identify the Commit to Move

First, identify the commit hash of the commit you want to move (a1b2c3d in our example). Take note of this hash, as we’ll use it in the subsequent steps.

Step 2: Checkout the Master Branch (Optional)

If you haven’t created the target branch (feature-b) yet, or you’re not already on the correct branch, switch to the master branch using the following command:

git checkout master

Step 3: Create and Checkout the Target Branch (Optional)

Create a new branch (feature-b) if it doesn’t exist and switch to it:

git checkout -b feature-b

Step 4: Cherry-Pick the Commit

Ensure that you are on the correct target branch (feature-b) and cherry-pick the commit onto this branch:

git checkout feature-b

git cherry-pick a1b2c3d

Step 5: Reset the Source Branch

Reset the source branch (feature-a) to the previous commit hash, effectively removing the mistakenly committed changes:

git checkout feature-a

git reset --hard <previous_commit_hash>

Conclusion:

git work tree is a detailed article about the flow of git work tree . While moving a commit to another branch in Git may sound daunting, it’s a routine task that can be easily accomplished locally. By following these simple steps, you can ensure that your commits are organized correctly, even if you’ve made a mistake in the initial branch selection. Remember, only perform these actions if you haven’t pushed the commits to the remote repository to avoid any conflicts. Here is a detailed article about renaming the git branch you can have a quick look Happy coding!

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

  • React Native vs Flutter: How to Pick the Best Framework for Your App in 2025
  • PostgreSQL JSON: 7 Must-Know Techniques for Effortless JSON Queries
  • React 18: 10 Powerful Features You Absolutely Must Know (2024 Guide)
  • Long Hair and Beard: 9 Fascinating Secrets Behind Programmers’ Iconic Look
  • Web App vs Website: 7 Powerful Differences You Must Know for Your Digital Success

Categories

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

Footer

  • About Us
  • Privacy Policy
  • Contact Us

Copyright © 2025 ยท The code Mood