Git
Programming
Coding Tips
Version Control
Debugging

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.

Browse interview questions

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.

bash
git status
git branch --show-current
git log --oneline --decorate --graph -n 12

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:

bash
git log --oneline origin/main..main

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.

bash
1# On wrong branch, for one mistaken commit
2git reset --soft HEAD~1
3
4git switch feature/login-timeout
5git commit -m "Add idle timeout handling to session middleware"

--soft keeps changes staged. If you prefer unstaged changes, use --mixed.

bash
git reset --mixed HEAD~1

For multiple mistaken commits, reset by count:

bash
git reset --soft HEAD~3

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.

bash
1# Move commit to correct branch
2git switch feature/login-timeout
3git cherry-pick a1b2c3d
4
5# Undo on main with a new commit
6git switch main
7git revert a1b2c3d
8git push origin main feature/login-timeout

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.

bash
1# You are on main but these commits belong elsewhere
2git switch -c feature/payment-retry
3
4# Return main to remote tip
5git switch main
6git reset --hard origin/main

Use this only when you are sure main local commits should be removed from main. If unsure, make a backup branch first.

bash
git branch backup/main-before-cleanup

Recover Safely with reflog

If something goes wrong, reflog is the fastest recovery tool because it tracks where HEAD pointed recently.

bash
git reflog -n 20
# Pick a prior safe state
git reset --hard HEAD@{3}

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 main with 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:

bash
1#!/usr/bin/env bash
2branch=$(git branch --show-current)
3if [ "$branch" = "main" ]; then
4  echo "Direct commits to main are blocked. Use a feature branch."
5  exit 1
6fi

Save as .git/hooks/pre-commit and mark executable with chmod +x.

Common Pitfalls

  • Running git reset --hard before 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 reflog when recovery is possible and instead redoing work manually.

Summary

  • Choose the fix based on push status and branch sharing rules.
  • For local-only mistakes, reset plus recommit is usually simplest.
  • For pushed shared history, prefer cherry-pick plus revert.
  • Use reflog as your safety net for almost all history accidents.
  • Add lightweight branch protections to prevent repeated errors.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.