Discarding a tracked file change with git restore is for edits that should be thrown away before they are committed. It restores only the paths you name, so another modified file in the same checkout can stay untouched.
git restore writes file content back from a restore source. Without –staged or –source, Git restores the working-tree file from the index, which usually matches HEAD when the file has not been staged.
Staged changes need a different command because the index already contains the unwanted content. Review the diff before restoring, and use the staged-and-worktree form only when the selected path should return to HEAD in both places.
Related: How to revert a Git commit
$ git status --short M docs/notes.md M docs/todo.md
The left column reports staged changes and the right column reports working-tree changes. A leading space followed by M means the file is modified only in the working tree.
$ git diff -- docs/notes.md diff --git a/docs/notes.md b/docs/notes.md index d135ccd..3455db0 100644 --- a/docs/notes.md +++ b/docs/notes.md @@ -1,2 +1,2 @@ Title: Restore notes -Status: reviewed +Status: draft
git restore overwrites the selected tracked path. Save the edit elsewhere or stash it first if any part may still be needed. Related: How to stash uncommitted changes in Git
$ git restore -- docs/notes.md
No output means Git restored the path without conflicts. The -- separator keeps the following argument interpreted as a file path.
$ git status --short M docs/todo.md
The restored docs/notes.md path no longer appears, while docs/todo.md is still listed because it was not part of the restore command.
$ cat docs/notes.md Title: Restore notes Status: reviewed
$ git status --short -- docs/notes.md MM docs/notes.md
MM means the path has staged content in the index and another edit in the working tree. Related: How to unstage changes in Git
$ git restore --source=HEAD --staged --worktree -- docs/notes.md
This command discards staged and unstaged content for the selected path. It does not remove untracked files. Related: How to remove untracked files with git clean
$ git status --short -- docs/notes.md
No output means the selected path matches the index and HEAD for the checked state.