How To Undo A Rebase In Git?

Tim Mouskhelichvili
Tim Mouskhelichvili
2 minutes to read

Let's say that after a successful rebase, you change your mind and want to undo the operation. Don't panic because Git offers an easy solution for this task.

To undo a rebase, find the last commit before the rebase and use the git reset command, like so:

bashgit reset --hard <hash>

This article will explore different scenarios of undoing a rebase and answer some of the most common questions.

Let's get to it 😎.

git undo rebase

Definition

In Git, rebasing means re-committing the commits from your active branch onto a new base commit.

The rebase operation is typically used when a developer wants to add the changes of the base branch to a child branch.

For example, you can use a rebase to add the changes inside the master branch to a feature branch.

How to undo a rebase?

To undo a rebase, you need to use the git reset command with the last committed hash before the rebase.

Here is the process to follow:

1. First, you need to find the last commit's hash before the rebase. You can find the commit's hash using the git reflog command.

bash> git reflog

cda11d5 HEAD@{0}: rebase: commit 1
8272fde HEAD@{1}: rebase: commit 2
dc12a9c HEAD@{2}: checkout: added the checkbox

As you can see, in this case, the last commit is the HEAD@{2}.

2. Undo the rebase with the git reset command.

bashgit reset --hard HEAD@{2}

3. Push the changes to the remote.

bashgit push -f

Note: Since the remote history diverges from the local history, you need to use the git push command with the force option.

Note: Learn more about HEAD here.

How to undo a rebase after a push?

To undo a rebase on the remote, you need to undo the rebase locally and force push your local changes to the remote.

How to undo a rebase in progress?

To undo a rebase in progress, you need to abort the rebase (I wrote an extensive guide on this).

Final thoughts

As you can see, Git makes it very easy to undo a rebase.

Just use the git rebase command, and voila.

git undo rebase

Here are some other Git tutorials for you to enjoy:

Comments (4)
Reply to:
Italo July 11, 2023 09:28AM
Helped me a lot! A big thank you!
REPLY
rebaser June 21, 2023 04:24AM
But this change your branch to point specific commit, you practically lost your commits too. What\'s intended by undoing rebase, moving your commits to the old base.
REPLY
Basma June 12, 2023 12:36PM
THANK YOU!!
REPLY
Anon April 13, 2023 01:21AM
Much appreciated! Saved me hours, if not days of work.
REPLY