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


当前回答

我总是用这个:

git clean -fdx

对于一个非常大的项目,你也许想运行它几次。

其他回答

使用git reset HEAD <file>以卸开一个文件

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 -fdn

这将显示要删除的文件列表。 现在要实际删除那些使用此命令的文件 :

git clean -fd

删除此 repo + 子模块中的所有额外文件夹和文件

这让你和新克隆人处于同样的状态

git clean -ffdx

删除此回调中的全部额外文件夹和文件, 但不删除子模块

git clean -fdx

删除额外的文件夹, 但不包括文件( 如: 构建或日志文件夹) 。

git clean -fd

删除额外文件夹 + 忽略的文件(但不是新添加的文件)

如果文件没有被忽略, 还没有被检查, 那么它会留下来 。 请注意首都 X 。

git clean -fdX

新的互动模式

git clean

OK,OK, OK, OK, OK, OK, OK, OK, OK,OK, OK,OK,OK,OK,OK,OK,OK,OK,OK,OK,OK,OK,OK,OK,OK,OK,OK,OK,OK,OK,OK,OK,OK,OK,OK,OK,OK,OK,OK,OK,OK,OK,OK,OK,删除删除未跟踪的自定义文件及文件夹容易使用git在命令行中,按这样做:

git clean -fd

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

在本案中-f代表武力和-d代表目录...

所以,如果只删除文件,您可以使用-f仅 :

git clean -f

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

git clean -fd

还有,你也可以用-x添加被 git 忽略的文件。 如果您想要删除所有文件, 这将很有帮助 。

并添加-igit 请求您逐个删除文件的许可 。

如果您不确定并想要先检查事项,请添加-n旗帜。

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

我还创造了下面的图像 让它更令人难忘 尤其是我见过很多人混淆-f有时是用来清理文件夹或把它混为一谈的!


deleting unwanted untracked files and folder