Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
2 views
in DevOps and Agile by (19.7k points)

In Git, how do you rollback a rebase if you're not happy with it?

Git doesn't have dry-run for rebasing. If I did a rebase and have not pushed it yet, how do I rollback to before, as if it never happened?

1 Answer

+3 votes
by (62.9k points)

You can use the reflog to search out the first action before the rebase started then reset --hard back to that. e.g.

$ git reflog

b710729 HEAD@: rebase: some commit

5ad7c1c HEAD@: rebase: another commit

deafcbf HEAD@: checkout: moving from master to my-branch

...

$ git reset HEAD@ --hard

Now you probably should go back to before the rebase started.

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

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 a similar result, you could reset --hard to the origin branch:

$ git reset --hard origin/my-branch

If you probably did try this while you had other unpushed commits, then you'll 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).

by (110 points)
This was a lifesaver, thanks!

Related questions

+3 votes
2 answers
asked Aug 1, 2019 in DevOps and Agile by Han Zhyang (19.7k points)
+1 vote
1 answer
+4 votes
1 answer
0 votes
1 answer

Browse Categories

...