Skip to content
Mishel's Devnotes.

Committed to the Wrong Branch? Here's How to Move That Commit

You’ve finished a change, committed it, and moved on—then you notice the branch name. The commit went to the wrong branch. It’s a frustrating mistake, but you usually don’t need to copy your files or redo your work. You can move the commit to the branch where it belongs, then clean up the original branch safely.

You’ve finished a change, committed it, and moved on—then you notice the branch name. The commit went to the wrong branch. It’s a frustrating mistake, but you usually don’t need to copy your files or redo your work. You can move the commit to the branch where it belongs, then clean up the original branch safely.

For example, imagine you meant to commit a payment validation change to feature/payment, but accidentally committed it to main:

git commit -m "Add payment validation"

Here’s how to fix it.

Find the commit hash

First, look at your recent commits:

git log --oneline

You’ll see output similar to this:

a1b2c3d Add payment validation
9e8f7a6 Update checkout page

Copy the hash for the commit you want to move. In this example, it’s a1b2c3d.

Apply the commit to the correct branch

Switch to the branch where you meant to make the commit:

git switch feature/payment

Then apply the commit with git cherry-pick:

git cherry-pick a1b2c3d

Git applies the changes from that commit to your current branch and creates a new commit there. You can check that it worked with:

git log --oneline

At this point, the change is on feature/payment. The original commit is still on the branch where you accidentally made it, so you may want to remove it there too.

Clean up the wrong branch

What you do next depends on whether you’ve pushed the accidental commit.

If the commit is only local

If you haven’t pushed the commit and it’s the latest commit on the wrong branch, switch back to that branch:

git switch main

Then remove the latest commit:

git reset --hard HEAD~1

Be careful with git reset --hard: it moves the branch back one commit and discards uncommitted changes in your working tree. Make sure you’re on the correct branch and don’t have uncommitted work you need before running it.

If the commit has already been pushed

If you’ve pushed the commit to a shared branch, avoid rewriting its history when other people may already have based work on it. Use git revert instead:

git revert a1b2c3d

This creates a new commit that reverses the changes from the original commit. The original commit remains in the history, but its changes are undone.

The quick version

To move a commit to the correct branch:

git log --oneline
git switch feature/payment
git cherry-pick <commit-hash>

Then clean up the wrong branch. For an unpushed latest commit, git reset --hard HEAD~1 can remove it. For a commit that’s already pushed to a shared branch, git revert <commit-hash> is usually safer.

Before using either command, check which branch you’re on and whether you have uncommitted changes.

Enjoyed this article? Explore more notes on engineering and the tools behind it.

Browse all articles