How to fix committing to the wrong Git branch?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Committing to the wrong Git branch is common, even for experienced developers working quickly across multiple tasks. The right recovery method depends on whether the commit was pushed and whether teammates already based work on it. This guide gives safe, practical workflows for each case.
Diagnose the Situation First
Before running history-changing commands, inspect repository state and identify exactly which commits are misplaced.
You need three facts:
- current branch name
- commit hashes that belong on another branch
- whether those commits are already on remote
Check push state with this command:
If this prints commits, local main is ahead of remote origin/main.
Case One: Commit Is Local and Not Pushed
If the bad commit exists only locally, the cleanest fix is to move the branch pointer back while keeping file changes, switch branches, then commit correctly.
--soft keeps changes staged. If you prefer unstaged changes, use --mixed.
For multiple mistaken commits, reset by count:
Then recommit on the right branch, or split into several commits with git add -p.
Case Two: Commit Was Pushed to Shared Branch
If the wrong commit is already on remote shared history, avoid rewriting unless your team explicitly agrees. Use cherry-pick to move the change and revert to undo it on the wrong branch.
This preserves an auditable timeline and avoids forcing teammates to repair local history after a force push.
Case Three: You Need to Move a Stack of Commits
Sometimes you committed an entire work stream on the wrong branch. Create a new branch at current HEAD, then restore the original branch to the correct remote tip.
Use this only when you are sure main local commits should be removed from main. If unsure, make a backup branch first.
Recover Safely with reflog
If something goes wrong, reflog is the fastest recovery tool because it tracks where HEAD pointed recently.
This works even after resets, as long as garbage collection has not pruned old objects.
Prevent Future Wrong-Branch Commits
Small guardrails reduce mistakes significantly:
- show current branch in shell prompt
- protect
mainwith server-side rules - require pull request merges
- enable local pre-commit checks that fail on protected branches
Example pre-commit hook that blocks direct commits to main:
Save as .git/hooks/pre-commit and mark executable with chmod +x.
Common Pitfalls
- Running
git reset --hardbefore confirming whether local files contain uncommitted work. - Force pushing shared branches without team coordination.
- Cherry-picking merge commits without understanding parent selection.
- Forgetting to verify which branch is currently checked out before cleanup commands.
- Skipping
reflogwhen recovery is possible and instead redoing work manually.
Summary
- Choose the fix based on push status and branch sharing rules.
- For local-only mistakes,
resetplus recommit is usually simplest. - For pushed shared history, prefer
cherry-pickplusrevert. - Use
reflogas your safety net for almost all history accidents. - Add lightweight branch protections to prevent repeated errors.
Related reading
- How to fix committing to the wrong Git branch?
- How to fix corrupted interactive rebase?
- How to fix 'Kafka Offset commit failed on partition The request timed out
- How to fix modified content, untracked content in git?
- How to fix dial unix /var/run/docker.sock connect permission denied when group permissions seem correct?
- How to fix Django error DisallowedHost at / Invalid HTTP_HOST header ... You may need to add ... to ALLOWED_HOSTS
- How to fix ssh connect to host github.com port 22 Connection timed out for git push/pull/... commands?
- How to get back to most recent version in Git?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.