git reset
Reset current HEAD to the specified state
The 'git reset' command is used to undo changes by moving the current branch HEAD to a specified commit. It has three main modes: --soft (keeps changes staged), --mixed (default - keeps changes in working directory), and --hard (discards all changes). This command is essential for undoing commits, unstaging files, and managing your commit history. Be careful with 'git reset --hard' as it permanently discards changes.
Synopsis
git reset [-q] [<tree-ish>] [--] <pathspec>...
git reset [-q] [--pathspec-from-file=<file>] [<tree-ish>]
git reset (--patch | -p) [<tree-ish>] [--] [<pathspec>...]
git reset [--soft | --mixed [-N] | --hard | --merge | --keep] [-q] [<commit>]Options
Choosing the Right Option
Reset Modes: What Gets Preserved?
Understanding the three main reset modes and what happens to your changes
| Command | HEAD moves | Staging Area | Working Directory | Use When |
|---|---|---|---|---|
--soft | Yes | Preserved (staged) | Preserved | Undo commit, want to re-commit with changes |
--mixed (default) | Yes | Cleared (unstaged) | Preserved | Undo commit, want to edit before staging |
--hard | Yes | Cleared | Tracked edits LOST (untracked kept) | Completely discard commit and all changes |
Reset vs Revert vs Checkout
Choosing the right undo command for your situation
| Command | Modifies History? | Safe for Pushed Commits? | Best For |
|---|---|---|---|
git reset | Yes (moves branch) | No - causes issues | Undoing local commits not yet pushed |
git revert | No (adds new commit) | Yes - safe for shared branches | Undoing pushed commits on shared branches |
git checkout -- file | No | Yes | Discarding uncommitted changes to specific files |
Unstaging Changes: Reset vs Restore
Modern git provides git restore as an alternative to reset for unstaging
| Command | Purpose | Affects Commits? | Recommended |
|---|---|---|---|
git reset HEAD file | Unstage a file | No | Traditional way |
git restore --staged file | Unstage a file | No | Modern way (Git 2.23+) |
git reset | Unstage all files | No | Works well |
git restore --staged . | Unstage all files | No | More explicit intent |
Advanced Reset Modes
When to use --merge and --keep instead of --hard
| Command | Preserves Local Changes? | Aborts If Conflicts? | Use Case |
|---|---|---|---|
--hard | No - destroys all | No | Complete reset, don't care about changes |
--merge | Unstaged changes only (staged are lost) | Yes | Abort a merge while keeping unrelated work |
--keep | Yes (in files the reset doesn't touch) | Yes | Undo commit but keep unrelated local edits |
Examples
Undo last commit, keep changes staged
git reset --soft HEAD~1Moves HEAD back one commit. Changes from that commit remain staged, ready to be re-committed.
Undo last commit, keep changes in working directory
git reset HEAD~1Default (--mixed) mode. Moves HEAD back one commit. Changes are preserved but unstaged.
Completely discard last commit and changes
git reset --hard HEAD~1Moves HEAD back one commit and discards ALL uncommitted changes to tracked files. Use with caution - lost uncommitted work cannot be recovered (the commit itself stays in git reflog).
Unstage a specific file
git reset HEAD file.jsRemoves file.js from the staging area without discarding changes. The file remains modified in the working directory.
Workflow Examples
git log --oneline -3See recent commits to confirm which to undo
git reset --soft HEAD~1Undo commit, keep changes staged
git statusVerify changes are staged
git commit -m "Better message"Recommit with new message
git statusSee what's currently staged
git reset HEAD unwanted-file.jsUnstage specific file(s)
git statusConfirm file is now unstaged
git commitCommit only the files you want
git statusReview what will be lost
git stash(Optional) Save changes just in case
git reset --hard HEAD~1Discard last commit AND all tracked changes (untracked files stay)
git statusConfirm clean working directory
git reflogFind the commit hash before the reset
git reset --hard abc1234Reset back to that commit hash
git log --onelineVerify commits are restored
Troubleshooting
fatal: ambiguous argument 'HEAD~1': unknown revision
Cause: You're on the initial commit - there's no previous commit to reset to
Check git log. If only one commit exists, you can't reset further back. Use git update-ref -d HEAD to completely restart.
error: Entry 'app.js' not uptodate. Cannot merge.
Cause: A file that --merge or --keep would need to change has uncommitted local edits, so the reset is aborted
Either commit/stash your changes first, or use --hard if you want to discard them
Accidentally used --hard and lost changes
Cause: Hard reset discards all uncommitted work permanently
If the changes were committed before: git reflog to find commit, then git reset --hard <hash>. If never committed: changes are likely lost forever.
Reset worked but teammate says history is broken
Cause: You reset commits that were already pushed to a shared branch
For shared branches, use git revert instead. To fix: git reflog, find original commit, reset to it, force push (coordinate with team).
fatal: ambiguous argument 'HEAD' when running git reset HEAD file
Cause: The repository has no commits yet, so HEAD does not exist (or the path is wrong)
Drop HEAD: git reset file.js (or git rm --cached file.js). Check the exact path with git status.
Tips & Pitfalls
Best Practices
- ✓Never use git reset on commits that have been pushed to shared branches - use git revert instead
- ✓Always run git status before reset to understand what will be affected
- ✓Prefer --soft when you want to redo a commit with modifications
- ✓Use git stash before --hard reset if you might want your changes back
- ✓Remember git reflog can recover from most reset mistakes (for committed changes)
- ✓Use git reset HEAD <file> to unstage files instead of resetting entire commits
- ✓Consider git restore --staged as a modern alternative for unstaging files
Common Mistakes
- ✗Using 'git reset --hard' without realizing it permanently deletes uncommitted changes. Always double-check before using --hard.
- ✗Resetting commits that have already been pushed to a shared remote. This rewrites history and causes problems for collaborators. Use 'git revert' instead.
- ✗Confusing 'git reset' with 'git revert'. Reset moves the branch pointer backward; revert creates a new commit that undoes changes.
- ✗Not knowing you can recover from a bad reset using 'git reflog' to find the previous commit hash.
- ✗Using 'git reset --hard HEAD' to discard working changes when 'git checkout -- .' or 'git restore .' would be more appropriate.
FAQ
Soft keeps changes staged. Mixed (default) keeps changes in working directory but unstaged. Hard discards everything. Think of it as: soft = undo commit only, mixed = undo commit + unstage, hard = undo everything.
For committed changes: yes, use git reflog to find the original commit hash, then reset to it. For uncommitted changes lost to --hard: usually no, those are gone permanently.
HEAD~1 means 'one commit before HEAD'. HEAD~2 is two commits back, etc. You can also use commit hashes directly like git reset abc1234.
Reset for local commits not yet pushed - it rewrites history cleanly. Revert for pushed commits on shared branches - it creates a new commit that undoes changes without rewriting history.
Use git reset HEAD filename to unstage a specific file. This doesn't move HEAD or affect any commits, just removes the file from the staging area.
It permanently deletes uncommitted changes. Unlike committed changes (recoverable via reflog), work that was never committed cannot be recovered after a hard reset.