How to undo git reset

Accidentally resetting to the wrong commit can seem catastrophic, especially with hard resets that appear to delete commits and changes. As the creator of CoreUI with over 25 years of software development experience, I’ve recovered from numerous accidental resets using Git’s safety mechanisms. Git’s reflog maintains a history of all reference updates, allowing you to recover commits even after seemingly destructive operations. This approach lets you undo any reset operation and restore your repository to its previous state.

Use Git reflog to find and restore commits after accidental reset operations.

# View reflog to see reset history
git reflog

# Output shows:
# abc1234 HEAD@{0}: reset: moving to HEAD~3
# def5678 HEAD@{1}: commit: Latest work
# ghi9012 HEAD@{2}: commit: Previous commit

# Restore to before reset
git reset --hard HEAD@{1}

# Or use the commit hash
git reset --hard def5678

Recover from different reset types:

# Undo soft reset (easy - changes still staged)
git reflog
git reset HEAD@{1}

# Undo mixed reset (changes in working directory)
git reflog
git reset HEAD@{1}

# Undo hard reset (appears to lose changes)
git reflog
git reset --hard HEAD@{1}  # Fully restores everything

Find specific commit to restore:

# Show reflog with commit messages
git reflog show --pretty=oneline

# Find when you had the desired state
git reflog | grep "commit message text"

# View commit details
git show HEAD@{n}

Partial recovery:

# Restore specific files from before reset
git checkout HEAD@{1} -- path/to/file.js

# Cherry-pick commits that were reset
git cherry-pick abc1234

Create new branch from lost commit:

# Create branch pointing to lost commit
git branch recovery-branch HEAD@{1}

# Or use commit hash
git branch recovery-branch abc1234

# Switch to recovery branch
git checkout recovery-branch

Check reflog for specific branch:

# View reflog for current branch
git reflog show HEAD

# View reflog for specific branch
git reflog show main

# View reflog for all references
git reflog show --all

Best Practice Note

The reflog is your safety net—it stores HEAD movements for 90 days by default. Even git reset --hard can be undone using reflog. Use git reflog immediately after realizing a mistake to find the commit hash you need. Reflog is local to your repository and not pushed to remotes. Each clone has its own reflog. For team repositories, communicate before recovering and force pushing. Use HEAD@{n} notation where n is how many moves back, or use commit hashes directly. This is our safety protocol in CoreUI development—reflog recovery for accidental resets, ensuring no work is permanently lost.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author