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.

Steps to discard tracked file changes with git restore:

  1. Check which tracked files have local changes.
    $ 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.

  2. Review the target file before discarding it.
    $ 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

  3. Discard the working-tree change for the selected file.
    $ 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.

  4. Confirm that only the unrelated file remains modified.
    $ 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.

  5. Check the restored file content when the file itself is the proof.
    $ cat docs/notes.md
    Title: Restore notes
    Status: reviewed
  6. Check for staged content before discarding a path that was already added to the index.
    $ 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

  7. Restore both the staged copy and the working-tree copy from HEAD when both should be discarded.
    $ 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

  8. Verify that the selected path no longer appears in short status.
    $ git status --short -- docs/notes.md

    No output means the selected path matches the index and HEAD for the checked state.