Reset a single Git file to HEAD
Restore one file in both the index and working tree, distinguish that from unstaging, and understand what Git can recover after an accidental overwrite.
To discard both staged and unstaged changes to one tracked file, restore it from HEAD into the index and working tree:
git diff -- path/to/file.txt
git diff --cached -- path/to/file.txt
# Run only after saving any changes you want to keep.
git restore --source=HEAD --staged --worktree -- path/to/file.txt
git restore requires Git 2.23 or later. The command does not move the branch or change unrelated paths. It overwrites the selected file's local changes; Git may have no recoverable copy of changes that were never staged or committed.
Choose the source and destinations
The older equivalent for a file present in HEAD is:
git checkout HEAD -- path/to/file.txt
# Use a historical version instead:
git checkout a1b2c3d -- path/to/file.txt
# Restore several files:
git restore --source=HEAD --staged --worktree -- src/a.js src/b.js
restore removes a tracked path that is absent from the source tree. Traditional checkout uses overlay behavior by default, so its handling of paths absent from that tree is not identical.
| Intent | Command |
|---|---|
| Discard unstaged changes; keep the staged version | git restore -- path/to/file.txt |
| Unstage a file; preserve its working copy | git restore --staged -- path/to/file.txt |
| Reset both copies to HEAD | git restore --source=HEAD --staged --worktree -- path/to/file.txt |
| Undo a committed change with another commit | git revert COMMIT |
git reset --hard HEAD affects the entire checkout. It is not the single-file form of reset, and it can remove untracked paths that obstruct writing tracked files.
Keep paths unambiguous
The -- separator marks the start of path arguments. For example, git checkout dev switches branches when dev is a branch, whereas git checkout -- dev restores the path from the index. Quote paths containing spaces as well.
For selected hunks, use git restore -p --source=HEAD --staged --worktree -- path/to/file.txt. Review each prompt. You can also stash selected work first with git stash push -- path/to/file.txt; ordinary stash does not include untracked files.
Recover from an accidental reset
If content was staged, its blob may still exist and git fsck --lost-found can help locate dangling objects. Filenames and the complete previous working state are not guaranteed. For content Git never stored, check editor history or external backups. A restore does not create a reflog entry containing the overwritten file contents.
These operations are local. Publishing a later commit is a separate action.
See git-restore and git-reset.
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan