如何从当前工作树中删除未追踪的本地文件?


当前回答

对于我刚刚发明并尝试的这种情况的一种生活哈克(这非常好):

git add .
git reset --hard HEAD

注意! 请确保在执行此事之前进行任何必要的更改(即使在未被拖动的文件中)。

其他回答

如果需要从特定子目录中删除未追踪的文件,

git clean -f {dir_path}

并结合的方式来删除未追踪的Dir/文件和被忽略的文件。

git clean -fxd {dir_path}

此后,您将仅在 git 状态中修改文件。

对于我来说,只有下一个工作:

git clean -ffdx

在所有其他情况下,我收到一些子办公室的“Skipping Directory”消息。

在运行清洁命令之前,总是使用 -n,因为它会向您展示哪些文件会被删除。

通常情况下,当没有确定的情况下,Git Clean 不会返回未跟踪的目录,以避免删除太多。 指定 -d 以便返回此类目录。 如果任何路径是确定的, -d 是无关紧要的; 所有未跟踪的文件符合确定的路径(除非在 --force 中提到的未跟踪的Git目录)将被删除。

git clean -n -d 
git clean -n -d -f

git clean -d -f

默认情况下, git clean 只会删除未追踪的文件,这些文件不会被忽视. 任何符合您的.gitignore 或其他 ignore 文件中的模式的文件将不会被删除. 如果您想要删除这些文件,您可以将 -x 添加到清洁命令。

git clean -f -d -x

git clean -x -i

If you are not 100% sure that deleting your uncommitted work is safe, you could use stashing instead
git stash --all

git stash push --keep-index

呼叫 git stash 没有任何论点,相当于 git stash push。

git stash push -m "name your stash" // before git stash save (deprecated)

git stash drop // or clean

要查看关于如何使用 stash 的完整指示,请参见此 如何在 git 中以名称命名和获取 stash?

git add --all, git stash and git stash drop, try these three commands in this order in order to remove all untracked files. 通过添加所有这些未追踪的文件到 git 并 stashing 它们将移动所有这些未追踪的文件到 stash 列表并下载顶部一个,即, stash@{0} 将从 stash 列表中删除所有未追踪的文件。

git-clean - 从工作树中删除未追踪的文件