Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I discard unstaged changes in Git?

How do I discard changes in my working copy that are not in the index?

like image 828
readonly Avatar asked Sep 09 '08 19:09

readonly


People also ask

How do I get rid of unstaged changes?

For all unstaged files in current working directory use: git restore . That together with git switch replaces the overloaded git checkout (see here), and thus removes the argument disambiguation. If a file has both staged and unstaged changes, only the unstaged changes shown in git diff are reverted.

What is unstaged changes in git?

Unstaged changes are changes that are not tracked by the Git. For example, if you copy a file or modify the file. Git maintains a staging area(also known as index) to track changes that go in your next commit.


1 Answers

For all unstaged files in current working directory use:

git checkout -- . 

For a specific file use:

git checkout -- path/to/file/to/revert 

-- here to remove ambiguity (this is known as argument disambiguation).

For Git 2.23 onwards, one may want to use the more specific

git restore . 

resp.

git restore path/to/file/to/revert 

that together with git switch replaces the overloaded git checkout (see here), and thus removes the argument disambiguation.

like image 94
Tobi Avatar answered Sep 28 '22 12:09

Tobi