gotchabashgitMajorpending
Gotcha: Git rebase onto wrong branch
Viewed 0 times
rebase recoveryreflogreset hardrebase abortwrong branch
Error Messages
Problem
Accidentally rebased onto the wrong branch, duplicating commits or losing work.
Solution
Recovery from bad rebase:
Prevention:
# DURING rebase (something looks wrong)
git rebase --abort # Undo completely, go back to before rebase
# AFTER rebase completed (already finished)
# Use reflog to find the pre-rebase state
git reflog
# Look for: 'rebase (start): checkout <branch>'
# The entry BEFORE that is your pre-rebase state
# abc1234 HEAD@{5}: rebase (start): checkout main
# def5678 HEAD@{6}: commit: My work <- THIS ONE
git reset --hard def5678 # Go back to pre-rebase state
# If you already pushed the bad rebase
# Create a backup branch first!
git branch backup-bad-rebase
git reset --hard def5678
git push --force-with-lease # Overwrite remote with correct historyPrevention:
# Always create a backup before risky operations
git branch backup-feature
git rebase main
# If something goes wrong:
git reset --hard backup-feature
# Use --onto for targeted rebases
# Rebase only commits from feature, not from old-base
git rebase --onto new-base old-base feature
# Verify before force pushing
git log --oneline main..HEAD # See what commits will be pushedWhy
Rebase rewrites commit history. Once you reset to a reflog entry, the rewritten commits become unreachable and will be garbage collected.
Context
Recovering from incorrect git rebase operations
Revisions (0)
No revisions yet.