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


当前回答

要删除未跟踪的文件, 您应该首先使用命令查看将受到清理影响的文件

git clean -fdn

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

git clean -fd

其他回答

git add --all, git stashgit stash drop,在此顺序中尝试这三个命令以删除所有未跟踪的文件。如果将所有这些未跟踪的文件添加到 Git 并隐藏它们,则所有未跟踪的文件都将移到隐藏列表中,并删除最上面的列表,即,隐藏%0}将从隐藏列表中删除隐藏的更改 。

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

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

git clean -ffdx

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

git clean -fdx

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

git clean -fd

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

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

git clean -fdX

新的互动模式

git clean

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

Remove .classpath [y/N]? N
Remove .gitignore [y/N]? N
Remove .project [y/N]? N
Remove .settings/ [y/N]? N
Remove src/com/arsdumpgenerator/inspector/ [y/N]? y
Remove src/com/arsdumpgenerator/manifest/ [y/N]? y
Remove src/com/arsdumpgenerator/s3/ [y/N]? y
Remove tst/com/arsdumpgenerator/manifest/ [y/N]? y
Remove tst/com/arsdumpgenerator/s3/ [y/N]? y

- i 用于交互式
-f 用于武力
-d 用于目录
- 被忽略文件的x( 需要时添加)

注: 添加添加添加- n,n-- 干燥运行检查它能做什么

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如果您真的想要删除此目录, 选项将两次 。