git reset

Basic Snapshotting

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>]
[ ] optional| or<arg> placeholder... repeatable--flag option

Options

Click any option to expand

Beginner (3)

Resets HEAD to the specified commit but keeps all changes staged. Your working directory and staging area remain unchanged. Use this to undo commits while keeping changes ready to recommit.

When neededUndo commits but keep the changes staged — e.g. to recommit or squash

Example

git reset --soft HEAD~1

The default mode. Resets HEAD and the staging area, but keeps changes in the working directory. Use this to unstage changes while keeping them available for editing.

RecommendedSafest way to undo a local commit or unstage; keeps your edits

Example

git reset HEAD~1

Resets HEAD, the staging area AND the working directory to the given commit. Uncommitted changes to tracked files are discarded, and untracked files that are in the way of the commit’s files are overwritten (other untracked files are left alone). There is no undo for uncommitted changes.

Not recommendedDestroys uncommitted changes to tracked files — stash or commit first

Example

git reset --hard HEAD~1

Intermediate (1)

Interactively select hunks to unstage. Allows you to selectively remove specific changes from the staging area while keeping others staged.

When neededUnstage selected hunks interactively

Example

git reset -p

Advanced (2)

Resets the index and updates files that differ between the commit and HEAD, but keeps unstaged changes. Staged changes are discarded. Aborts if a file that must change has unstaged edits.

When neededAborts a merge; discards staged changes but keeps unstaged ones

Example

git reset --merge HEAD~1

Resets the index and updates files that differ between commit and HEAD. Aborts if any file that differs has local changes. Preserves uncommitted changes to files unchanged between commits.

When neededSafer than --hard for moving back: refuses to overwrite local edits

Example

git reset --keep HEAD~1

Choosing the Right Option

Reset Modes: What Gets Preserved?

Understanding the three main reset modes and what happens to your changes

CommandHEAD movesStaging AreaWorking DirectoryUse When
--softYesPreserved (staged)PreservedUndo commit, want to re-commit with changes
--mixed (default)YesCleared (unstaged)PreservedUndo commit, want to edit before staging
--hardYesClearedTracked edits LOST (untracked kept)Completely discard commit and all changes

Reset vs Revert vs Checkout

Choosing the right undo command for your situation

CommandModifies History?Safe for Pushed Commits?Best For
git resetYes (moves branch)No - causes issuesUndoing local commits not yet pushed
git revertNo (adds new commit)Yes - safe for shared branchesUndoing pushed commits on shared branches
git checkout -- fileNoYesDiscarding uncommitted changes to specific files

Unstaging Changes: Reset vs Restore

Modern git provides git restore as an alternative to reset for unstaging

CommandPurposeAffects Commits?Recommended
git reset HEAD fileUnstage a fileNoTraditional way
git restore --staged fileUnstage a fileNoModern way (Git 2.23+)
git resetUnstage all filesNoWorks well
git restore --staged .Unstage all filesNoMore explicit intent

Advanced Reset Modes

When to use --merge and --keep instead of --hard

CommandPreserves Local Changes?Aborts If Conflicts?Use Case
--hardNo - destroys allNoComplete reset, don't care about changes
--mergeUnstaged changes only (staged are lost)YesAbort a merge while keeping unrelated work
--keepYes (in files the reset doesn't touch)YesUndo commit but keep unrelated local edits

Examples

Undo last commit, keep changes staged

git reset --soft HEAD~1

Moves 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~1

Default (--mixed) mode. Moves HEAD back one commit. Changes are preserved but unstaged.


Completely discard last commit and changes

git reset --hard HEAD~1

Moves 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.js

Removes file.js from the staging area without discarding changes. The file remains modified in the working directory.

Workflow Examples

1
git log --oneline -3

See recent commits to confirm which to undo

2
git reset --soft HEAD~1

Undo commit, keep changes staged

3
git status

Verify changes are staged

4
git commit -m "Better message"

Recommit with new message

1
git status

See what's currently staged

2
git reset HEAD unwanted-file.js

Unstage specific file(s)

3
git status

Confirm file is now unstaged

4
git commit

Commit only the files you want

1
git status

Review what will be lost

2
git stash

(Optional) Save changes just in case

3
git reset --hard HEAD~1

Discard last commit AND all tracked changes (untracked files stay)

4
git status

Confirm clean working directory

1
git reflog

Find the commit hash before the reset

2
git reset --hard abc1234

Reset back to that commit hash

3
git log --oneline

Verify 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

A:

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.

A:

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.

A:

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.

A:

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.

A:

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.

A:

It permanently deletes uncommitted changes. Unlike committed changes (recoverable via reflog), work that was never committed cannot be recovered after a hard reset.