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


当前回答

在实际删除之前先知道要删除的内容:

git clean -d -n

它将产生类似的东西:

将删除样本. txt

删除上一个命令输出中列出的所有内容 :

git clean -d -f

它将产生类似的东西:

删除样本. txt

其他回答

git clean -f

将会从当前 git 中删除未跟踪的文件

git clean -fd

当您想要删除目录和文件时, 这将只删除未跟踪的目录和文件

(d) [-f] [-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 [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <path>…​

说明说明

通过递归式删除未在版本控制下的文件来清除工作树,从当前目录开始.

通常, 只有 Git 未知的文件才会被删除, 但是如果-x选项被指定,被忽略的文件也会被删除。例如,这可以用于删除所有构建产品。

任何可选的或可选的<path>...给出了参数,只有这些路径受到影响。


步骤1是用-n选项 :

# Print out the list of files and directories which will be removed (dry run)
git clean -n -d

清洁步骤 -注意: 这将删除文件:

# Delete the files from the repository
git clean -f
  • 要删除目录,运行git clean -f -dgit clean -fd
  • 要删除被忽略的文件,运行git clean -f -Xgit clean -fX
  • 要删除被忽略和未丢弃的文件,请运行git clean -f -xgit clean -fx

Note案件差别X后两项命令。

如果(如果)clean.requireForce设置为您的配置中的“ true”(默认),需要指定-f否则不会真的发生任何事。

再次见git-clean需要更多信息的医生。


选项选项选项

-f, --force

如果 Git 配置变量为 clean. requireForce 不被设定为假, 除非给定, 否则 Git 配置变量为 clean.requireForce 将拒绝运行-f, -n-i.

-x

不要使用标准忽略规则,$GIT_DIR/info/exclude,但是仍然使用-e选项。这样可以删除所有未跟踪的文件, 包括构建产品。 这可以用来( 可能与 git 重设结合) 创建一个纯净的工作目录, 以测试一个干净的构建 。

-X

只删除被 Git 忽略的文件。 这也许有助于从零开始重建一切, 但保存手动创建的文件 。

-n, --dry-run

不要删除任何东西, 只显示会做什么。

-d

除未跟踪的文件外,删除未跟踪的目录。如果一个未跟踪的目录由不同的 Git 仓库管理,则默认不会删除。使用-f如果您真的想要删除此目录, 选项将两次 。

git clean -f -d -x $(git rev-parse --show-cdup)对根目录应用干净, 无论您在仓库目录树中将其命名为哪里。 我总是使用它, 因为它不会迫使您离开您现在工作所在的文件夹, 并允许您从所在位置清除并承诺 。

确定该旗帜-f, -d, -x匹配您的需要 :

-d
       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.

-f, --force
       If the Git configuration variable clean.requireForce is not set to
       false, git clean will refuse to delete files or directories unless
       given -f, -n or -i. Git will refuse to delete directories with .git
       sub directory or file unless a second -f is given. This affects
       also git submodules where the storage area of the removed submodule
       under .git/modules/ is not removed until -f is given twice.

-x
       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.

还有其他的旗帜 也有其他的,只是检查git clean --help.

我感到惊讶的是,以前没有人提到这一点:

git clean -i

这代表着交互式互动您将快速了解要删除的内容, 以便您能够包含/ 排除受影响的文件。 总的来说, 仍然比执行强制任务要快--dry-run在真正的清洁之前。

你将不得不扔在-d如果您也想要处理空文件夹。最后,它会制作一个漂亮的别名:

git iclean

话虽如此,对有经验的用户来说,持有互动命令的手掌外握,可能会使有经验的用户感到疲劳。git clean -fd