Oscar the owl and Vershd logo
Menu

How to Git Move Commit to Another Branch Effortlessly

The Problem

It's too early in the day, and the tea / coffee / taurine hasn’t kicked in yet. You’ve made some commits, and now realise they’re on entirely the wrong branch, master, to be exact. You can already hear the pedants in the office tutting.

You need to move these Git commits to another branch, either a new branch, or an existing one. Here’s how to do this.

The Solution for a New Branch

In this example, there are 3 commits to move.

This creates the new branch my-branch, containing all the commits:
git move commit to another branch
git branch my-branch
This changes master. It deletes the last 3 commits, and resets the index and working files:
git reset --hard HEAD~3
Or: To stop the reset if there are changed working files that would get destroyed by --hard, then use --keep instead:
git reset --keep HEAD~3
This makes my-branch the current branch, so you can work on it:
git checkout my-branch
Warning: If after doing this you need to rebase this new branch onto the master, you may need to use --no-fork-point to protect your moved commits, otherwise they might get erased.

The Solution for an Existing Branch

In this example, there are 4 commits to move.

This switches you to the branch you want the commits on, my-branch:
git checkout my-branch
This merges the master into my-branch, bringing all its commits:
git merge master
This makes master the current branch again:
git checkout master
This changes master. It deletes the last 4 commits, and resets the index and working files:
git reset --hard HEAD~4
Or: To stop the reset if there are changed working files that would get destroyed by --hard, then use --keep instead:
git reset --keep HEAD~4
This switches you back to my-branch to admire the results:
git checkout my-branch
If you want the simplest and easiest solution to this problem though, why not try our Git client.

Zero stress Git

Vershd is an effortless Git GUI that boosts software development. It works on Windows, macOS, and Linux. It's free for personal, educational, non-profit and trade union use.

Designed for intermediate and beginner Git users, our unique UI/UX boosts your software development and makes Git simple.

Help & tools

We provide these pages to try to make your programming life easier. Our resources page gives an overview. You can see a full list of our Git tips & tools here.

We have these free tools:

Customers say...

I love any tool that makes my life easier... This is perfect, just the right amount of control. No more, no less. Easy to get started, push and rollback changes... It's a no brainer!
Hayden T.
Oscar the owl and Vershd logo
 © 2021 Blightysoft
Made in England
St. George's flag of England, the Union Jack flag of the United Kingdom, the European Union flag, and the United Nations flag.
chevron-right linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram