引用自https://docs.gitlab.com/ee/topics/git/numerous_undo_possibilities_in_git/
At this point there are 3 options to undo the local changes you have:
Discard all local changes, but save them for possible re-use later:
git stash
Discarding local changes (permanently) to a file:
git checkout -- <file>
Discard all local changes to all files permanently:
git reset --hard
上面这些方法都会保留unstaged files
https://stackoverflow.com/questions/52704/how-do-i-discard-unstaged-changes-in-git
如果要discard unstaged files, using
git clean -df
usage: git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>...
-q, --quiet do not print names of files removed
-n, --dry-run dry run
-f, --force force
-i, --interactive interactive cleaning
-d remove entire directories
-e, --exclude <pattern>
add <pattern> to ignore rules
-x remove ignored files, too
-X remove only ignored files
Another quicker way is:
usage: git stash list [<options>]
or: git stash show [<stash>]
or: git stash drop [-q|--quiet] [<stash>]
or: git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]
or: git stash branch <branchname> [<stash>]
or: git stash save [--patch] [-k|--[no-]keep-index] [-q|--quiet]
[-u|--include-untracked] [-a|--all] [<message>]
or: git stash [push [--patch] [-k|--[no-]keep-index] [-q|--quiet]
[-u|--include-untracked] [-a|--all] [-m <message>]
[-- <pathspec>...]]
or: git stash clear
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.
more details could be seen in https://git-scm.com/docs/git-stash