Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git how to rollback a rebase

Tags:

git

People also ask

Can I undo git rebase?

To undo the rebase , we can use the reflog command of Git. Using git reflog , we can determine the branch's head commit immediately before the rebase starts.

Which command is used to undo a git rebase?

Another option is to bypass the commit that caused the merge failure with git rebase --skip . To check out the original <branch> and remove the . git/rebase-apply working files, use the command git rebase --abort instead.


You can use the reflog to find the first action before the rebase started and then reset --hard back to it. e.g.

$ git reflog

b710729 HEAD@{0}: rebase: some commit
5ad7c1c HEAD@{1}: rebase: another commit
deafcbf HEAD@{2}: checkout: moving from master to my-branch
...

$ git reset HEAD@{2} --hard

Now you should be back to before the rebase started.

To find the right place to reset to, you just pick the entry closest to the top that doesn't start with "rebase".

Update: As mentioned in comments and other answers, you can also use ORIG_HEAD as an easier way to find where to reset to: git reset ORIG_HEAD --hard

Alternative approach

If the rebase is the only thing you have done on the branch, i.e. you have no unpushed commits/changes - then you could just delete the local branch with git branch -D and then check it out again:

$ git checkout my-branch
$ git rebase master
// not happy with the result
$ git checkout master
$ git branch -D my-branch
$ git checkout my-branch

Or for the same effect, you could reset --hard to the origin branch:

$ git reset --hard origin/my-branch

If you did do this while you had other unpushed commits, then you will have lost them. In that case, just use the reflog approach above to jump back to the reflog entry where you made the commit(s).


Rebase keeps a backup of the old state as ORIG_HEAD.
So you can revert the last rebase by running:

git reset --hard ORIG_HEAD