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.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github