How to undo git rebase
Undoing a git rebase is essential when a rebase goes wrong, introduces conflicts, or produces unexpected results. As the creator of CoreUI with over 25 years of software development experience, I’ve recovered from many problematic rebases in production repositories. Git’s reflog maintains a complete history of branch tip movements, allowing you to reset to the state before the rebase. This approach safely recovers your original branch state even after a completed or aborted rebase.
Use git reflog to find the commit before rebase and reset your branch to undo the rebase operation.
Undo completed rebase:
# You just finished a rebase but want to undo it
git rebase main
# Rebase completed, but something went wrong
# View reflog to find commit before rebase
git reflog
# Output shows:
# abc1234 HEAD@{0}: rebase finished: returning to refs/heads/feature
# def5678 HEAD@{1}: rebase: commit message 3
# ghi9012 HEAD@{2}: rebase: commit message 2
# jkl3456 HEAD@{3}: rebase: commit message 1
# mno7890 HEAD@{4}: rebase: checkout main
# pqr1234 HEAD@{5}: commit: Your last commit before rebase
# Reset to commit before rebase started
git reset --hard HEAD@{5}
# Or use the commit hash
git reset --hard pqr1234
Undo rebase with conflicts:
# You started a rebase and got conflicts
git rebase main
# CONFLICT (content): Merge conflict in file.js
# If you want to abort the rebase
git rebase --abort
# This returns you to the state before rebase
# If you already aborted but want to verify
git reflog
git reset --hard HEAD@{1}
Find the exact commit before rebase:
# Use reflog with more details
git reflog show --pretty=oneline
# Look for line starting with 'rebase: checkout'
# The commit immediately before this is your pre-rebase state
# Show details of that commit
git show HEAD@{5}
# Compare with current state
git diff HEAD@{5} HEAD
Undo interactive rebase:
# You did interactive rebase and want to undo
git rebase -i HEAD~5
# After modifying commits, you want to go back
git reflog
# Find the commit before 'rebase -i (start)'
# abc1234 HEAD@{0}: rebase -i (finish): returning to refs/heads/feature
# def5678 HEAD@{5}: rebase -i (start): checkout HEAD~5
# ghi9012 HEAD@{6}: commit: Last commit before rebase
git reset --hard HEAD@{6}
Undo rebase on remote branch:
# You rebased and force pushed
git rebase main
git push --force
# To undo, reset locally
git reflog
git reset --hard HEAD@{5}
# Force push again (dangerous - coordinate with team)
git push --force-with-lease
# Better: create recovery branch first
git branch backup-before-force-push
git push --force-with-lease
Create backup branch before risky rebase:
# Before rebasing, create backup
git branch backup-before-rebase
# Do the rebase
git rebase main
# If something goes wrong
git reset --hard backup-before-rebase
# Clean up backup when done
git branch -d backup-before-rebase
Recover specific commits from bad rebase:
# If rebase lost some commits
git reflog
# Find the lost commit hash
# abc1234 HEAD@{10}: commit: Important feature
# Cherry-pick the lost commit
git cherry-pick abc1234
# Or create branch from that commit
git branch recovered-work abc1234
Check what rebase changed:
# Before undoing, see what rebase did
git reflog
# Find commit before rebase (e.g., HEAD@{5})
# Compare current state with pre-rebase
git diff HEAD@{5} HEAD
# View commit history changes
git log --oneline HEAD@{5}..HEAD
Abort rebase in progress:
# If rebase is still in progress
git rebase --abort
# This safely returns to pre-rebase state
# Verify with status
git status
Best Practice Note
Always use git rebase --abort if you want to stop during a rebase—it’s safer than manual recovery. Create a backup branch before rebasing important work. The reflog keeps rebase history for 90 days by default. Use git reflog show to see detailed history with timestamps. After undoing a rebase, verify with git log and git status that you’re at the correct state. If you’ve already force pushed after rebase, coordinate with your team before force pushing the undo. This is how we handle rebases in CoreUI development—maintaining reflog awareness, creating backups before risky operations, and knowing recovery procedures for when rebases go wrong.



