Guide
How to undo a git rebase
A rebase rewrites commits, but the old ones are still in your repository. Undoing it means pointing your branch back at the commit it was on before.
If the rebase is still in progress
If git stopped for a conflict and you want out, abort. This puts the branch and your working tree back exactly as they were before you started:
git rebase --abort
Right after a finished rebase: ORIG_HEAD
When a rebase finishes, git saves the branch’s previous position in ORIG_HEAD. If you haven’t run another reset, merge or rebase since, this is the quickest fix:
git reset --hard ORIG_HEAD
reset --hard discards uncommitted changes. Run git status first. If anything is modified, commit it or run git stash before resetting.
Any time later: the reflog
ORIG_HEAD is overwritten by the next command that moves HEAD a long way, so for anything older, use the reflog. It records every position HEAD has been in:
git reflog
a1b2c3d HEAD@{0}: rebase (finish): returning to refs/heads/feature
a1b2c3d HEAD@{1}: rebase (pick): Add settings page
9f8e7d6 HEAD@{2}: rebase (start): checkout main
4c5d6e7 HEAD@{3}: commit: Add settings pageFind the rebase (start) line for the rebase you want to undo. The entry just below it, here HEAD@{3}, is where your branch was before the rebase. Check it, then reset to it:
git log --oneline -5 'HEAD@{3}' # make sure this is the history you want
git reset --hard 'HEAD@{3}'The quotes stop shells like zsh from treating the braces as a pattern. You can also use the short SHA, e.g. git reset --hard 4c5d6e7.
The branch has its own reflog too, which skips the individual picks: git reflog show feature. The entry before rebase (finish) is the old tip.
If you already pushed the rebased branch
Reset locally as above, then force-push the old history back. Use --force-with-lease, which refuses if someone else pushed in the meantime:
git push --force-with-lease
Anyone who pulled the rebased commits will need to reset their copy too, so tell them before you push.
How long you have
Reflog entries are kept for 90 days by default (30 for commits no branch points to), after which git gc may delete the old commits. Undo sooner rather than later.