# Undo a Git Rebase ## Quickest: ORIG_HEAD `git reset`, `git rebase`, and `git merge` all save the previous HEAD to `ORIG_HEAD`: ```bash git reset --hard ORIG_HEAD ``` Caveat: only works if you haven't run another reset/rebase/merge since. ## Using reflog Find the commit immediately before the rebase started: ```bash git reflog ``` Look for the entry just before `rebase (start)` — **not** the `rebase (start)` line itself: ```bash b7d29f8 HEAD@{8}: commit: fix bug ← this one b6b63c7 HEAD@{7}: rebase (start): checkout b6b63c7 ``` Then reset to it: ```bash git reset --hard HEAD@{8} # or using the branch reflog shorthand (one operation back): git reset --hard @{1} ``` ## If rebase is still in progress ```bash git rebase --abort ``` If you get `Interactive rebase already started`, use `git rebase --abort` (without `-i`). ## If commits were garbage-collected Use `--onto` to replay the topic branch on the old base: ```bash git rebase --onto <old-base-commit> <master> <topic> ``` This takes all commits on `topic` not on `master` and replays them on `<old-base-commit>`. ## Links - [Stack Overflow — Undoing a git rebase](https://stackoverflow.com/questions/134882/undoing-a-git-rebase#135614) - [[Interactive Rebase]]