version control - How do I discard unstaged changes in Git?

ID : 121253

viewed : 30

Tags : gitversion-controlgit

Top 5 Answer for version control - How do I discard unstaged changes in Git?

vote vote

99

For all unstaged files in current working directory use:

git restore . 

For a specific file use:

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.

If a file has both staged and unstaged changes, only the unstaged changes shown in git diff are reverted. Changes shown in git diff --staged stay intact.

Before Git 2.23

For all unstaged files in current working directory:

git checkout -- . 

For a specific file:

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

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

vote vote

82

Another quicker way is:

git stash save --keep-index --include-untracked 

You don't need to include --include-untracked if you don't want to be thorough about it.

After that, you can drop that stash with a git stash drop command if you like.

vote vote

74

It seems like the complete solution is:

git clean -df git checkout -- . 

WARNING: while it won't delete ignored files mentioned directly in .gitignore, git clean -df may delete ignored files residing in folders.

git clean removes all untracked files and git checkout clears all unstaged changes.

vote vote

66

This checks out the current index for the current directory, throwing away all changes in files from the current directory downwards.

git checkout . 

or this which checks out all files from the index, overwriting working tree files.

git checkout-index -a -f 
vote vote

57

git clean -df 

Cleans the working tree by recursively removing files that are not under version control, starting from the current directory.

-d: Remove untracked directories in addition to untracked files

-f: Force (might be not necessary depending on clean.requireForce setting)

Run git help clean to see the manual

Top 3 video Explaining version control - How do I discard unstaged changes in Git?

Related QUESTION?