To undo a Git rebase, follow these steps depending on your situation:
1. Undo a Rebase in Progress (Before Completion)
If the rebase is ongoing (e.g., conflicts not resolved), abort it:
git rebase --abort
2. Undo a Completed Rebase
If the rebase finished but hasn’t been pushed, reset to the original state using ORIG_HEAD
:
git reset --hard ORIG_HEAD
ORIG_HEAD
points to the commit where HEAD was before the rebase started.
3. If ORIG_HEAD
is Unavailable (e.g., Closed Terminal)
Use the reflog to find the commit before the rebase:
git reflog
- Locate the entry like
rebase (start): checkout <hash>
orrebase (finish): ...
. - Reset to the commit before the rebase:
git reset --hard <commit-hash>
4. Undo a Rebase That Was Pushed to Remote
If you’ve already pushed the rebased commits, force-push the original history:
git push origin <branch-name> --force
# Or safer (checks for remote changes):
git push origin <branch-name> --force-with-lease
Example Workflow
# 1. Start an interactive rebase (5 commits back)
git rebase -i HEAD~5
# 2. Realize you made a mistake and abort:
git rebase --abort
# If rebase already completed:
git reflog
# Find the pre-rebase commit (e.g., abc123)
git reset --hard abc123
# Force-push if already pushed to remote:
git push origin main --force-with-lease
Key Notes
- ORIG_HEAD: Git automatically creates this reference during operations like rebase.
- Force-Pushing: Use cautiously if others have pulled the rebased branch.
- Lost Commits: If you can’t find the original commit in the reflog, use
git fsck --lost-found
to recover dangling commits.
Prevention Tips
- Avoid rebasing public/shared branches.
- Use
git merge
instead ofgit rebase
for collaborative workflows.