I Committed to the Wrong Branch
You finish a feature, type git commit, and then realise you were on main instead of your feature-x branch. This is one of the most common Git mistakes. The good news is that commits are just objects — you can move them around. This page covers every scenario from a simple local mistake to an already-pushed commit.
Understand the situation first
Check where you are and what you committed
git log --oneline -5 # abc1234 (HEAD -> main) Add user authentication ← this should be on feature-x # def5678 Merge pull request #12 # ghi9012 Update README git branch # * main # feature-x
Scenario A: The commit is local (not yet pushed)
This is the easiest case. The commit only exists on your machine. The plan is:
Create (or identify) the target branch so it points at the wrong commit.
Undo the commit from
mainwith a soft reset (keeps your changes staged).Switch to the feature branch.
Commit your staged changes there.
Method 1: branch + soft reset (recommended)
# Step 1: create feature-x pointing at the current commit # (skip this if feature-x already exists — we'll cherry-pick instead) git branch feature-x # feature-x is now pointing at abc1234 # Step 2: undo the commit from main, but keep the changes staged git reset HEAD~1 --soft # HEAD is now at def5678 # Your file changes are staged and ready # Step 3: switch to the feature branch git switch feature-x # Step 4: commit git commit -m "Add user authentication" # Done — the commit lives only on feature-x
Scenario B: feature-x already exists with other commits
If feature-x already exists and has its own history, use git cherry-pick to copy the commit, then reset it from main.
Method 2: cherry-pick + reset
# Note the hash of the commit you made by mistake git log --oneline -3 # abc1234 (HEAD -> main) Add user authentication ← wrong branch # def5678 Merge pull request #12 # Step 1: switch to the correct branch git switch feature-x # Step 2: copy the commit here git cherry-pick abc1234 # [feature-x xyz9999] Add user authentication # The commit is now on feature-x # Step 3: go back to main and remove the wrong commit git switch main git reset HEAD~1 --hard # HEAD is now at def5678 # (safe here because feature-x already has the commit)
Scenario C: Multiple commits on the wrong branch
Moving several commits
git log --oneline -6 # abc1234 (HEAD -> main) Add logout endpoint # bcd2345 Add login endpoint # cde3456 Add auth middleware ← these three belong on feature-auth # def4567 Merge PR #11 (correct base) # Step 1: create the feature branch at the current position git branch feature-auth # Step 2: reset main back to the correct base commit git reset def4567 --soft # All three commits' changes are now staged # Step 3: switch to the feature branch git switch feature-auth # Step 4: commit (or leave as individual commits if you prefer) git commit -m "Add authentication feature"
Scenario D: The commit has already been pushed
If the commit was pushed to a protected or shared branch, the safest approach is to NOT rewrite history. Instead:
Cherry-pick the commit to the correct feature branch.
Create a new commit on main that reverts the accidental commit.
Push both.
Safe fix for pushed commits
# Step 1: switch to the feature branch and cherry-pick git switch feature-x git cherry-pick abc1234 git push origin feature-x # Step 2: back on main, revert the accidental commit git switch main git revert abc1234 --no-edit # Creates a new commit: "Revert 'Add user authentication'" # Step 3: push the revert git push origin main # No force-push needed. History is additive, not rewritten.
Force-push approach (solo or pre-review branches only)
If you are the only person using the branch (a feature branch, not main), and you know nobody has pulled it, you can use force-push to clean up history:
Force-push approach — solo branches only
# Ensure the commit is on feature-x first git switch feature-x git cherry-pick abc1234 git push origin feature-x # Now rewrite main locally and force-push git switch main git reset abc1234^ --hard # reset to parent of the bad commit git push --force-with-lease origin main
Prevention: use branch protection
Enable branch protection on
mainin GitHub/GitLab — direct pushes require a PR.Set
git config branch.main.pushRemote no_pushto block accidental pushes to main from your machine.Use a shell prompt that always shows your current branch name.
Develop the habit of running
git branchorgit statusbefore committing.