git reset is a command for rewinding commit history. It has three options: --soft, --mixed (default), and --hard, each of which undoes changes to different extents.
Usage
# Reset to one commit before (default is --mixed)
git reset HEAD~1
# --soft: Only undo the commit, changes remain in the staging area
git reset --soft HEAD~1
# --mixed: Undo commit and staging, changes remain in the working directory (default)
git reset --mixed HEAD~1
# Or simply
git reset HEAD~1
# --hard: Undo everything, changes are completely lost (use with caution)
git reset --hard HEAD~1
Differences Between the Three Options
--soft: Only Undo the Commit
Only rewinds the commit history, leaving changes in the staging area (the state after git add).
# Reset to one commit before
git reset --soft HEAD~1
# In this state, changes remain in the staging area
git status
# Shows: Changes to be committed:
Use case: When you want to fix a commit message or combine multiple commits into one
--mixed: Undo Commit and Staging (Default)
Undoes the commit history and staging area, leaving changes in the working directory.
# Reset to one commit before (--mixed can be omitted)
git reset --mixed HEAD~1
# Or
git reset HEAD~1
# In this state, changes remain in the working directory
git status
# Shows: Changes not staged for commit:
Use case: When you want to undo a commit, re-edit the changes, and commit again
--hard: Undo Everything
Undoes the commit history, staging area, and working directory. All changes are completely lost.
# Reset to one commit before
git reset --hard HEAD~1
# In this state, changes are completely lost
git status
# Shows: nothing to commit, working tree clean
Use case: When you want to completely discard incorrect changes (Note: uncommitted changes are also lost)
Usage Guidelines
Want to Fix a Commit Message → --soft
# Undo the last commit
git reset --soft HEAD~1
# Change the commit message and recommit
git commit -m "Fixed commit message"
Want to Re-edit Changes Before Committing → --mixed (Default)
# Undo the last commit
git reset HEAD~1
# Edit files
# ...
# Stage changes and recommit
git add .
git commit -m "Commit message after re-editing"
Want to Completely Discard Changes → --hard
# Completely discard the last commit and changes
git reset --hard HEAD~1
# Note: Uncommitted changes are also lost
Important Notes
--hard is Dangerous
Since --hard completely loses changes, please note the following:
- Uncommitted changes are also lost: Changes in the working directory are also deleted
- Difficult to restore: Once lost, changes cannot be restored unless you use Git's reflog (
git reflog) - Do not use on shared branches: Using
--hardon commits that have already been pushed will affect other developers
Safe Way to Undo
If you want to undo a commit that has already been pushed, use git revert instead of git reset:
# Create a new commit that undoes the commit (safe)
git revert HEAD