如何从工作目录删除所有更改,包括新的未跟踪文件。我知道git checkout -f这样做,但它不会删除自上次提交以来创建的新的未跟踪文件。

有人知道怎么做吗?


当前回答

如果希望放弃所有更改,可以在.gitconfig中的别名中使用任何有效选项。例如:

[alias]
    discard = "!f() { git add . && git stash && git stash drop stash@{0}; }; f"

用法:git discard

其他回答

如果希望放弃所有更改,可以在.gitconfig中的别名中使用任何有效选项。例如:

[alias]
    discard = "!f() { git add . && git stash && git stash drop stash@{0}; }; f"

用法:git discard

git reset --hard # removes staged and working directory changes

## !! be very careful with these !!
## you may end up deleting what you don't want to
## read comments and manual.
git clean -f -d # remove untracked
git clean -f -x -d # CAUTION: as above but removes ignored files like config.
git clean -fxd :/ # CAUTION: as above, but cleans untracked and ignored files through the entire repo (without :/, the operation affects only the current directory)

要查看将事先删除的内容,而不实际删除它,请使用-n标志(这基本上是一个测试运行)。当你准备实际删除时,然后删除-n标志:

Git clean -nfd

看看git clean命令。

git-clean -从工作树中删除未跟踪的文件 从当前目录开始,递归地删除不受版本控制的文件,从而清理工作树。 通常情况下,只有git不知道的文件会被删除,但是如果指定了-x选项,忽略的文件也会被删除。例如,这对于删除所有构建产品很有用。

Git clean -i将首先向您显示要删除的项目,并在确认后继续执行。我发现这在处理不应该被意外删除的重要文件时很有用。

有关更多信息,请参阅git help clean,包括一些其他有用的选项。

对于我使用的特定文件夹:

git checkout -- FolderToClean/*