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


当前回答

使用使用git clean -f -d以确保目录目录目录目录也删除。

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

    git clean -n
    

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

    git clean -fd
    

然后您可以检查您的文件是否真的已删除git status.

其他回答

清除 Git 仓库和所有子模块

以下命令将清除当前 git 仓库及其所有子模块的递归 :

(git clean -d -x -f && git submodule foreach --recursive git clean -d -x -f)

我喜欢git stash push -u因为你可以用git stash pop.

我还找到了一种方法 在藏物中显示未追踪的文件(例如:git show stash@{0}^3) https://stackoverflow.com/a/12681856/338986

内容2:git stash save将作有利于push谢谢 @timp-wolf.

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

git clean -d -n

它将产生类似的东西:

将删除样本. txt

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

git clean -d -f

它将产生类似的东西:

删除样本. txt

我总是用这个:

git clean -fdx

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

(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