πŸ€–Save up to $132K/month in CI costs!Try Freeβ†’
Skip to main content

How to use Git Reset --hard: A Complete Guide with Examples

6 min read
DevOps Engineer, Former Frontend Engineer πŸ”§
My focus is on automating workflows and making sure CI/CD pipelines run like a well-oiled machine.

This article was last updated on January 22, 2025, to include advanced strategies for safely using git reset –hard, managing complex multi-branch scenarios, recovering from accidental resets, and providing clearer, simplified explanations for better understanding.

TL;DR

What is git reset --hard?
Think of git reset --hard as a time machine for your code - it lets you jump back to any previous commit, but be careful: it will erase all changes made after that point!

When to use git reset --hard?

  • When you need to completely start fresh from a specific commit
  • When your local changes are messy and you want to match the remote exactly
  • When you're absolutely sure you don't need any of your current uncommitted changes

Warning: This command can permanently delete your work. Always commit or stash important changes first!

After accidentally losing several hours of work early in my career (yes, I'm looking at you, git reset --hard), I've learned to both respect and master this powerful Git command. Let me share what I've learned the hard way, so you don't have to.

Steps we will cover:

What is Git Reset Hard and How Does It Work?​

Think of Git like a time machine that takes you back in time with your code. Normal git reset was like going back in time but bringing all the current changes along in some bag. However, git reset --hard is more like going back in time and erasing completely everything which happened after that point.

Click to zoom

I remember the first time this command and I met. I was working on this feature that had taken hours, made a mess of my local changes, and then this guy says, "Just do a git reset --hard!" Let's just say I learned my lesson about commits that day.

Interactive Git Reset Hard Tutorial​

Safely play with different git reset scenarios in this interactive tool:

Git Reset Explorer

abc123
Initial commit
  • Add README.md
def456
Add feature
  • Add feature.js
ghi789
Fix bug
  • Update feature.js

Working Directory

  • Uncommitted change 1
  • Uncommitted change 2

Staged Changes

  • Staged change 1

What's happening?

This interactive tool demonstrates how git reset --hard works:

  • The timeline shows your commit history
  • HEAD points to your current commit
  • Working directory shows uncommitted changes
  • Staged changes are ready to be committed
  • When you reset --hard, you'll lose all uncommitted changes!
  • Use "Start Over" to reset the entire visualization

Common Git Reset Hard Use Cases​

Let me give a few real-world scenarios of using git reset --hard:

First, fetch the latest changes

git fetch origin

Reset to match remote branch exactly

git reset --hard origin/main

Reset to a specific commit

git reset --hard abc123

Or go back N commits

git reset --hard HEAD~3  # Goes back 3 commits

Always check what will be affected

git status

See what changes you'll lose

git diff

Create a backup branch just in case

git branch backup-before-reset

Check the reflog for your previous HEAD position

git reflog

Reset back to where you were

git reset --hard HEAD@{1}

DON'T do this on shared branches

git reset --hard origin/main

DO THIS FIRST

git fetch origin

Then reset

git reset --hard origin/main

Git Reset Hard Safety Guide​

After my early disasters with git reset --hard I always maintain these safety steps:

Check Your Status First

# Always check what will be affected
git status

# See what changes you'll lose
git diff

Backup Branch Creation

# Create a backup branch just in case
git branch backup-before-reset

How to Recover from Git Reset Hard​

It doesn't look too good, does it? But all is not lost even after a hard reset! Here are some recovery techniques I've used:

# Check the reflog for your previous HEAD position
git reflog

# Reset back to where you were
git reset --hard HEAD@{1}

Git Reset Hard Best Practices​

Here's what I've learned from years of using (and sometimes misusing) git reset --hard:

DoDon't
Always check git status firstReset without reviewing changes
Create backup branchesUse on shared branches
Apply on local changes onlyReset public/remote branches
Check the commit hashTrust your memory

Common Git Reset Hard Mistakes to Avoid​

Resetting Public Branches

This is one of the most dangerous mistakes you can make. If you reset a branch that others are using, it immediately creates conflicts for anyone who has pulled that branch. Since their history is different from yours, this will result in merge conflicts and possible loss of work.

# DON'T do this on shared branches
git reset --hard origin/main

Forgetting to Fetch First

Always fetch before resetting to a remote branch. If you don't, you could reset your work from the version in the local cache rather than getting critical updates made by your coworkers.

# DO THIS FIRST
git fetch origin

# Then reset
git reset --hard origin/main

Frequently Asked Questions about Git Reset Hard​

Common Questions

Q: Is it possible to undo a git reset --hard?
A: Yes, using git reflog-but only if the changes were committed first.

Q: Is git reset --hard ever safe to run?
A: Safe when used carefully on local branches; can permanently delete uncommitted changes.

Q: What is the difference between git reset --hard and git clean?
A: The command git reset --hard removes changes to tracked files, and git clean removes untracked files.

Conclusion​

Git reset --hard is kind of like power tools: it's very useful when it's used right but can be quite dangerous in the hands of an amateur. Start with small resets on local branches, always double-check what you're about to reset, and keep those backup branches handy.

Remember: in Git, as in life, better safe than sorry. Take my word for this, as well as I've learned all that by the hard way!