Skip to main content

Using Git

Git Move Commit to Another Branch

Sometimes you commit to an incorrect branch and now you want to move the commit to the correct branch. Here's how to handle the situation.

Sh(g)it happens. I mean it is usual to clone the main branch, create or switch to a dev branch and then commit the changes to this dev branch which is merged to the main later.

Imagine you follow the same only you forget to switch to the dev branch and you made the commit to the main branch.

But before pushing, you want to move this commit to the dev branch instead. You should also remove the commit from the main vranch.

Let me help you by showing the steps for:

  • Moving the commit to the correct branch
  • Reverting the commit from the incorrect branch

Moving commit to another branch

First, let's address the issue that I encountered:

While working with three branches, I was supposed to make one commit to the header branch and another to the footer branch.

The first commit to the header branch was correct but unfortunately, I made the second commit to the header branch instead of the footer branch:

Commit made to wrong branch

When I checked the git log, it was pretty clear to me that I made a commit to the wrong branch:

Checking git log

Now, let's take a look at the steps to move the commit to another branch.

Step 1: Find the hash of the commit

To find the hash of the commit you want to move, you can use the git log in the beach where you made a wrong commit.

I made a wrong commit in the head branch so I'll be using git log there:

git log
Find the hash of the commit

Once you find the hash, copy the hash.

Step 2: Switch to the target branch

Next, switch the branch in which you want to move the commit. For that purpose, you can use the git checkout command:

git checkout <branch-name>

In my case, I want to move to commit to the footer beach, so I'll be using the following:

git checkout footer
Switch branch in git

Step 3: Move the commit to the target branch

Once you switch to the target branch, use the git cherry-pick command along with the hash you copied from the first step:

git cherry-pick <hash>
Move git commit to another branch using cherry-pick

To verify if the commit was moved or not, you can check the git log:

git log
Verify if the commit was moved successfully to correct branch

There you go!

Revert the incorrect commit

When you use the cherry-pick command, it does not move the commit but copies the commit to the current branch. So you are still left with the incorrect commit on the first branch.

The solution is to revert the incorrect commit.

For that, first switch to the branch in which you made the incorrect commit:

git checkout <branch-name>

In my case, the beach name was header so I will be using the following:

git checkout header

Now, if you check the git log, you will still find the incorrect commit which you recently moved using the git cherry-pick command:

Incorrect commit is still in the first branch

To revert this commit, you append the hash of the target commit to the git revert command as shown here:

git revert <hash>

It will open the text editor telling you it is reverting the commit. It creates another commit without those changes resulting removal of the specified commit.

Close the text editor and that's it:

Once you close the text editor, you will see an output telling you that the commit has been deleted:

Revert incorrect git commit

There you have it!

Wrapping Up

In this tutorial, I went through how you can move your commit to a different branch and also explained how you can remove the incorrect commit.

I hope you will find this guide helpful. If you have any queries, feel free to leave us a comment.

Sagar Sharma