How do I undo 'git reset'?

Better Stack Team
Updated on July 25, 2024

Undoing a git reset can be tricky depending on the type of reset you performed (--soft, --mixed, --hard) and the specific context. Here are steps for each scenario:

1. Soft or Mixed Reset

If you used git reset --soft or git reset --mixed, you can easily undo the reset:

  1. Find the Commit SHA Before the Reset:

    • Use the git reflog command to see a history of your commands and their corresponding commit SHAs.

       
      git reflog
      
 
- Locate the SHA of the commit before the reset.
  1. Reset Back to the Previous Commit:

    • Once you have identified the correct commit SHA, you can reset back to it.

       
      git reset --soft <commit_SHA>
      

2. Hard Reset

If you used git reset --hard, this is more complex because this type of reset discards changes in the working directory and the staging area.

  1. Find the Commit SHA Before the Reset:

    • Use git reflog to identify the commit SHA before the reset.

       
      git reflog
      
  2. Reset Back to the Previous Commit:

    • Reset to the previous commit using the SHA identified.

       
      git reset --hard <commit_SHA>
      

Example: Undoing a Reset Step-by-Step

  1. Run git reflog to Get History:

     
    git reflog
    

    You might see something like:

     
    a1b2c3d (HEAD -> main) HEAD@{0}: reset: moving to a1b2c3d
    e4f5g6h HEAD@{1}: commit: Added new feature
    i7j8k9l HEAD@{2}: commit: Fixed bug
    
  2. Identify the Commit Before Reset:

    • Let's say you reset to a1b2c3d, and the commit before that was e4f5g6h.
  3. Undo the Reset:

    • To undo, you would reset back to e4f5g6h.

       
      git reset --hard e4f5g6h
      

Notes:

  • Data Loss Risk: If you performed a hard reset and had uncommitted changes, they are likely lost unless you can recover them from a backup or a stash.
  • Backup and Caution: Always ensure you have a backup or use stashes (git stash) before performing potentially destructive operations like git reset --hard.

By carefully using git reflog, you can navigate through your command history and correct the effects of a reset.

Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

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