Committed to Wrong Branch
Galat branch pe commit? Daro mat โ soft reset se undo karo, sahi branch pe redo karo.
The scenario: You are on main, make changes, commit them, and then realize โ this should have been on a feature branch. Sound familiar? This is the most common "Oh shit" moment in Git.
If you have NOT pushed: easy fix. Use git reset --soft HEAD~1 to undo the commit while keeping your changes staged.
If you have already pushed: harder fix. You must use git revert to create an "anti-commit" on the wrong branch, then git cherry-pick to copy the commit to the right branch.
The key tool for unpushed fixes: git reset --soft HEAD~1.
--soft moves the branch pointer back but keeps your changes in the staging area, ready to commit again. Nothing is lost.
# The disaster: committed on main instead of feature
git branch
# * main โ Oops! Should be on feature branch
echo "new feature code" > feature.js
git add feature.js
git commit -m "feat: add new feature"
# Realized you are on main! Do not panic.
git reset --soft is your best friend. It undoes the commit but keeps all your work. If you HAVE pushed, you must use git revert instead โ never force push to shared branches.Step 1: git reset --soft HEAD~1 โ Undo the last commit, but keep all changes staged.
Step 2: git stash โ Temporarily save the staged changes. This is the safest method to transport changes between branches.
Step 3: git checkout -b feature โ Create and switch to the correct branch.
Step 4: git stash pop โ Bring your changes back.
Step 5: git commit -m "feat: add new feature" โ Commit on the right branch.
Alternative to stash: After soft reset, just switch branches. If your changes do not conflict with the target branch, they travel with you to the new branch. But stash is safer โ no surprises.
# Step 1: Undo the commit, keep changes staged
git reset --soft HEAD~1
git status
# Changes to be committed:
# new file: feature.js
# Step 2: Stash the changes (safest approach)
git stash
# Step 3: Create and switch to the correct branch
git checkout -b feature/new-feature
# Step 4: Pop the stash
git stash pop
# Step 5: Commit on the correct branch
git commit -m "feat: add new feature"
# Verify: main is clean, feature has the commit
git log --oneline main # no feature commit
git log --oneline feature/new-feature # has the feature commit
git reset --soft HEAD~1, your changes are in the staging area. You can also skip the stash and directly git checkout -b feature โ the staged changes will follow you. But if anything goes wrong during the switch, stash is your safety net.If you already pushed the commit to the wrong branch, or prefer a different workflow, use git cherry-pick.
Cherry-pick copies a commit from one branch to another. The original commit stays where it is.
Step 1: Copy the hash of the wrong commit.
Step 2: Switch to the correct branch.
Step 3: git cherry-pick <hash> โ Apply the commit here.
Step 4: Switch back to the wrong branch and git revert <hash> to remove it.
This is the safest method if the commit has already been pushed.
# You committed on main (and maybe even pushed)
git log --oneline -1
# a1b2c3d feat: add new feature
# Step 1: Note the hash
HASH=$(git rev-parse HEAD)
# Step 2: Switch to the correct branch
git checkout -b feature/new-feature
# Step 3: Cherry-pick the commit
git cherry-pick $HASH
# The commit is now on feature branch too!
# Step 4: Remove it from main
git checkout main
git revert $HASH
# Creates an "anti-commit" that undoes the changes on main
git push origin main
# Verify
git log --oneline main # has the revert
git log --oneline feature/new-feature # has the original commit
git revert the original on main. If you only cherry-pick without reverting, both branches have the same changes โ which is not what you want.If the commit is already pushed to a shared branch (main, develop), you CANNOT use reset.
Using git reset on a pushed branch would require a force push, which overwrites team history. Your teammates would have broken repositories.
Instead, use git revert <hash> to create a new commit that undoes the changes. Then cherry-pick the original commit to the correct branch.
This preserves the history: anyone looking at the log sees the commit + revert, which is transparent and honest.
The only downside: your git log has a "revert" commit, which is a small price for safety.
# ALREADY PUSHED TO MAIN - DO NOT RESET!
# Bad: git reset --hard HEAD~1 && git push --force (destroys team history)
# Good: Revert + Cherry-pick
# 1. Get the hash
git log --oneline -1 # abc1234 feat: new feature
# 2. Revert on main (undo the changes)
git revert abc1234
git push origin main
# 3. Cherry-pick to correct branch
git checkout -b feature/new-feature
git cherry-pick abc1234
git push -u origin feature/new-featureThe best fix is prevention. Always check git branch or your prompt before committing.
Configure your terminal prompt to show the current branch name โ this one habit prevents 90% of wrong-branch commits.
Use git status before every commit โ it shows the current branch at the top.
Some teams use pre-commit hooks to warn if you are committing directly to main.
You can also set init.defaultBranch to something other than main if you frequently accidentally commit there.
# Prevention 1: Check before you commit
git branch # am I on the right branch?
git status # shows current branch at top
# Prevention 2: Show branch in terminal prompt
# Add to ~/.bashrc or ~/.zshrc:
# parse_git_branch() {
# git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
# }
# PS1="\w\$(parse_git_branch) $ "
# Prevention 3: Warn before committing to main
# Add to .git/hooks/pre-commit:
# BRANCH=$(git rev-parse --abbrev-ref HEAD)
# if [ "$BRANCH" = "main" ]; then
# echo "WARNING: You are committing to main!"
# read -p "Continue? (y/n) " -n 1 -r
# if [[ ! $REPLY =~ ^[Yy]$ ]]; then exit 1; fi
# fi
Lo kar liya โ Key Points:
- โ
If you commit to the wrong branch and haven't pushed: use
git reset --soft HEAD~1to undo the commit while keeping changes staged - โ After soft reset, stash the changes, switch to the correct branch, pop the stash, and commit
- โ
If the commit is already pushed: use
git cherry-pick <hash>to copy it to the right branch, thengit revert <hash>on the wrong branch - โ
Never use
git reseton a pushed, shared branch โ it requires force push and destroys team history - โ
git cherry-pickcopies a specific commit from one branch to another, creating a new commit with a new hash - โ
Prevent wrong-branch commits by always checking
git branchorgit statusbefore committing
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login