Undoing a Git Rebase

Better Stack Team
Updated on June 24, 2024

Undoing a Git rebase involves restoring the branch to its original state before the rebase. If the rebase was completed but not pushed to a remote repository yet, you can use the reflog to find the commit before the rebase and reset the branch to that commit. Here's how you can do it:

Step 1: Find the Commit Before the Rebase

 
git reflog

This command will display a list of recent actions, including the commits before and after the rebase. Look for the commit before the rebase.

Step 2: Reset the Branch

Once you've identified the commit before the rebase, use git reset to move the branch pointer back to that commit:

 
git reset --hard <commit-before-rebase>

Replace <commit-before-rebase> with the commit hash or reference before the rebase.

Note:

  • Be cautious when using git reset --hard, as it will discard any changes in your working directory and staging area.
  • If the rebase has been pushed to a remote repository and shared with others, you may need to coordinate with your collaborators to undo the changes appropriately.