Coding⏱️ 2 min read📅 2026-05-30

How to Fix: How to fix committing to the wrong Git branch

Undo last commit to master branch and transfer changes to upgrade branch.

Quick Answer: Use `git reset --hard HEAD~1` to discard the last commit, then use `git cherry-pick` or `git rebase` to apply the changes to the correct branch.

If you've accidentally committed changes to the wrong Git branch, don't panic. The good news is that you can easily undo the last commit and then merge those changes into your desired branch.

🛑 Root Causes of the Error

  • Committing to the wrong branch can occur due to a variety of reasons, such as:

🔧 Proven Troubleshooting Steps

Method 1: Revert Commit

  1. Step 1: Navigate to the branch where you made the incorrect commit using git checkout master. This will switch your working directory to the master branch.

Method 2: Revert Commit

  1. Step 1: Use git revert to create a new commit that reverses the changes made in the incorrect branch.

✨ Wrapping Up

Once you've reverted the commit, navigate back to your upgrade branch using git checkout upgrade. Then, use git merge master to merge the corrected changes into your upgrade branch.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions