Book Your Free Call

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 (0)