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


当前回答

在运行清洁命令之前,总是使用 -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?

其他回答

在运行清洁命令之前,总是使用 -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?

要知道在实际删除之前会删除什么:

清洁 -d -n

它会产生这样的东西:

将移除 sample.txt

要删除前命令输出中列出的所有内容:

清洁 -d -f

它会产生这样的东西:

删除 sample.txt

要删除未追踪的文件:

git add .
git reset --hard HEAD

用途: git clean [-d] [-f] [-i] [-n] [-q] [-e ] [-x♰ -X] [--]...

-q, --quiet           do not print names of files removed
-n, --dry-run         dry run
-f, --force           force
-i, --interactive     interactive cleaning
-d                    remove whole directories
-e, --exclude <pattern>
                      add <pattern> to ignore rules
-x                    remove ignored files, too
-X                    remove only ignored files

好吧,删除不需要的未追踪文件和文件夹是容易使用 git 在命令行,只是这样做:

git clean -fd

在做之前双检查,因为它会删除文件和文件夹,而不会创建任何历史。

所以,如果你只想删除文件,你只能使用 -f:

git clean -f

如果您想要删除(目录)和文件,您只能删除未追踪的目录和文件如下:

git clean -fd

并添加 -i 旗帜,使 git 请求您的许可,以便在行程中单独删除文件。

如果您不确定,并希望先检查事物,请添加 -n 旗帜。

使用 -q 如果您不希望在成功删除后看到任何报告。

我也创建下面的图像,使它更令人难忘,特别是我已经看到许多人混淆 -f 为清洁文件夹有时或混合它在某种程度上!