How to discard unstaged changes in a file?

git checkout path/to/file

To discard unstaged changes in a file use git checkout path/to/file, where path/to/file should be replaced with the path to your selected file.

Layer 1
Terminal Example
git status
On branch main
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git restore ..." to discard changes in working directory)
    modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")

git checkout README.md
Updated 1 path from the index

git status
On branch main
nothing to commit, working tree clean

See git checkout to learn more about it.

Last modified on November 30, 2021.

You might also like