Oscar the owl and Vershd logo
Menu

How to Git Move Commit to Another Branch Effortlessly

The Problem

It's the first hour of work, and the tea / coffee / taurine hasn’t kicked in yet. Having created some commits in Git, you now know they are on the wrong branch. Main, to be exact. You can already hear the pedants in the office tutting.

You need to shift 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 swift example, there are only 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.

Free

GitBreeze is an effortless Git GUI that's free for use at work, at home, anywhere. It boosts software development. It works on Windows, macOS, and Linux.

Designed for developers who want Git to be simple, our unique UI/UX boosts your software development - learn more and reap the benefits.

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
 © 2024 GitBreeze Ltd.
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