How do I discard unstaged changes in Git?
How do I discard changes in my working copy that are not in the index?
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.
Read more... Read less...
It seems like the complete solution is:
git clean -df git checkout -- .
git clean removes all untracked files (warning: while it won't delete ignored files mentioned directly in .gitignore, it may delete ignored files residing in folders) and
git checkout clears all unstaged changes.
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
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
git help clean to see the manual
My favorite is
git checkout -p
That lets you selectively revert chunks.
git add -p
Since no answer suggests the exact option combination that I use, here it is:
git clean -dxn . # dry-run to inspect the list of files-to-be-removed git clean -dxf . # REMOVE ignored/untracked files (in the current directory) git checkout -- . # ERASE changes in tracked files (in the current directory)
This is the online help text for the used
git clean options:
Remove untracked directories in addition to untracked files. If an untracked directory is managed by a different Git repository, it is not removed by default. Use
-f option twice if you really want to remove such a directory.
Don’t use the standard ignore rules read from
.gitignore (per directory) and
$GIT_DIR/info/exclude, but do still use the ignore rules given with
-e options. This allows removing all untracked files, including build products. This can be used (possibly in conjunction with
git reset) to create a pristine working directory to test a clean build.
Don’t actually remove anything, just show what would be done.
If the Git configuration variable
clean.requireForce is not set to
false, Git clean will refuse to delete files or directories unless given
-i. Git will refuse to delete directories within the
.git subdirectory or file, unless a second
-f is given.