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
If you committed on the wrong Git branch, the fix depends on whether the commit has already been pushed. Before pushing, you can usually move the commit cleanly with git switch, git cherry-pick, or git reset. After pushing, you should be more careful because history rewrites can affect other people.
If You Have Not Pushed Yet
A common clean fix is:
- create or switch to the correct branch
- move the commit there
- remove it from the wrong branch if needed
For example, if the wrong branch has the commit at HEAD:
In collaborative work, avoid --hard unless you are certain you only want to remove your unpushed mistaken commit from that branch.
Use cherry-pick When You Need to Copy a Commit
If the correct branch already exists, copy the commit there.
Then remove the commit from the wrong branch if appropriate.
This is often the clearest answer when you know exactly which commit should move.
Be More Careful If the Commit Was Already Pushed
Once the wrong-branch commit is pushed, rewriting branch history can disrupt teammates. In that situation, the safer answer is often to:
- cherry-pick the commit onto the right branch
- revert it on the wrong branch
That preserves shared history more safely than force-pushing a rewrite.
Common Pitfalls
- Using destructive reset commands without first checking whether the commit was pushed.
- Moving a commit to the correct branch but forgetting to remove or revert it on the wrong branch.
- Force-pushing rewritten history on a shared branch without coordination.
- Fixing the history mechanically without first deciding whether copy, move, or revert is the safer workflow.
- Panicking and running several Git commands before confirming the current branch and commit state.
Summary
- The right fix depends on whether the mistaken commit was pushed.
- For unpushed mistakes, moving or resetting is usually straightforward.
- '
git cherry-pickis the standard way to copy a commit to the correct branch.' - For pushed commits, prefer safer history-preserving fixes such as revert plus cherry-pick.
- Check the branch and publication state before choosing the command sequence.
Related reading
- 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 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?
- How to get back to the latest commit after checking out a previous commit?
- How to get Git to clone into current directory
- How to get just one file from another branch
.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.