Move Last Commit to Another Branch (not pushed)

If you just made a commit and realized it was on the wrong branch, use:

git reset HEAD~1
git checkout correct-branch

This will remove the unpushed commit so that you can make the change on the correct branch. Note: You will have to write your commit message again.

The best explanation of git reset I could find is this one by Ryan Lundy.

Note: There are a few caveats to this scenario, like if you have made changes since that commit that are unstaged.

My purpose in sharing this StackOverflow post and snippet is in case anyone searches for “moving” a commit to another branch, like I had searched initially.

Was this post helpful?