git reset Command Guide
Reset current HEAD to the specified state
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Modes
git reset --soft HEAD~1Undo commit, keep stagedgit reset HEAD~1Undo commit, unstagegit reset --hard HEAD~1Undo commit, discard allUnstage
git reset HEAD <file>Unstage filegit resetUnstage allRecovery
git reflogView historygit reset --hard HEAD@{1}Undo resetDownloadable Image Preview
Basic Usage
The git reset command moves the HEAD pointer and optionally modifies the staging area and working directory. It's used to unstage files, undo commits, or discard changes.
# Unstage all files
git reset
# Unstage specific file
git reset HEAD file.js
# Undo last commit, keep changes staged
git reset --soft HEAD~1Reset Modes
The three modes determine what happens to your changes:
| Mode | HEAD | Staging | Working Dir |
|---|---|---|---|
| --soft | Moved | Unchanged | Unchanged |
| --mixed (default) | Moved | Reset | Unchanged |
| --hard | Moved | Reset | Reset |
# --soft: Undo commit, keep changes staged
git reset --soft HEAD~1
# Ready to commit again with different message
# --mixed (default): Undo commit, unstage changes
git reset HEAD~1
# Changes are in working directory, not staged
# --hard: Undo commit, discard all changes
git reset --hard HEAD~1
# Everything is gone - be careful!--hard permanently discards uncommitted changes. There is no undo for changes that were never committed.Unstaging Files
# Unstage a file (keep changes in working directory)
git reset HEAD file.js
# Unstage all files
git reset
# Modern alternative (Git 2.23+)
git restore --staged file.jsUndoing Commits
# Undo last commit, keep changes staged
git reset --soft HEAD~1
# Undo last 3 commits
git reset --soft HEAD~3
# Undo to specific commit
git reset --soft abc1234
# Undo commit but keep changes to edit
git reset HEAD~1
# ... make edits ...
git add .
git commit -m "New message"Recovery with Reflog
Git keeps a log of where HEAD has been, allowing recovery from mistakes:
# View reflog
git reflog
# abc1234 HEAD@{0}: reset: moving to HEAD~1
# def5678 HEAD@{1}: commit: Add feature
# Recover from accidental reset
git reset --hard HEAD@{1}
# Or use the commit hash directly
git reset --hard def5678Practical Examples
# Oops, wrong commit message
git reset --soft HEAD~1
git commit -m "Correct message"
# Split a commit into two
git reset HEAD~1
git add file1.js
git commit -m "First change"
git add file2.js
git commit -m "Second change"
# Undo merge (not pushed)
git reset --hard HEAD~1
# Completely start over to match remote
git reset --hard origin/main
# Remove a file from staging
git reset HEAD secrets.jsonFrequently Asked Questions
What is the difference between --soft, --mixed, and --hard?
--soft keeps changes staged. --mixed (default) unstages changes but keeps them in working directory. --hard discards everything - use with caution!
How do I undo a reset?
Use "git reflog" to find the commit hash before the reset, then "git reset --hard <hash>" to return to that state. This works as long as the commits haven't been garbage collected.
What is the difference between reset and revert?
Reset rewrites history by removing commits. Revert creates a new commit that undoes changes. Use revert for shared branches (safer), reset for local work.
How do I unstage a file without reset?
You can use "git restore --staged <file>" (Git 2.23+) which is clearer. It does the same thing as "git reset HEAD <file>".
Is it safe to use git reset --hard?
It permanently discards uncommitted changes. Only use it when you're sure you don't need those changes. Committed work can usually be recovered via reflog.
Summary
git reset is powerful for undoing changes locally. Use --soft to redo commits, --hard with caution.
Quick Reference
git reset --soft HEAD~1- Undo commit, keep stagedgit reset HEAD~1- Undo commit, unstagegit reset --hard HEAD~1- Undo commit, discard allgit reset HEAD <file>- Unstage file
Official Documentation
For authoritative information, refer to the official documentation: