BudiBadu Logo
Samplebadu

Git by Example: Reset Hard

2.43.0

Learn how to destructively discard all local changes and revert your repository to a specific commit. This is a powerful command that should be used with caution.

Code

# 1. Check current status (dirty working directory)
git status

# Output:
# modified:   app.js
# new file:   temp.log

# 2. Hard reset to the previous commit
git reset --hard HEAD~1

# Output:
# HEAD is now at 1a2b3c Fix login bug

# 3. Verify status (working directory is clean)
git status

# Output:
# On branch main
# nothing to commit, working tree clean

# 4. Hard reset to match a remote branch exactly
git reset --hard origin/main

Explanation

The git reset --hard command is the most drastic and destructive version of the reset command. When you run it, Git does three things: it moves the HEAD pointer back to the specified commit, it resets the staging area to match that commit, and—most importantly—it overwrites your working directory to match that commit.

This means that any uncommitted changes you have in your files will be permanently lost. There is no undo button for uncommitted work destroyed by a hard reset. It is effectively a "factory reset" for your current branch, bringing it back to the exact state of a specific point in history.

You typically use this command when you want to completely abandon your current work and start over, or when you want to force your local branch to match a remote branch exactly, discarding any local divergence.

  • Moves HEAD pointer back
  • Resets Staging Area AND Working Directory
  • Permanently deletes uncommitted changes
  • Use with extreme caution

Code Breakdown

9
The command that wipes out all local changes and moves the branch tip back by one commit.
21
A common use case: forcing your local 'main' branch to match the remote 'origin/main' exactly, discarding any local commits or edits.