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


当前回答

普通普通git clean命令不会用我的git version 2.9.0.windows.1.

$ git clean -fdx     # doesn't remove untracked files
$ git clean -fdx *   # Append star then it works!

其他回答

我喜欢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 -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.

哦,我,我,我,Zshzzsh 千兆赫通过 git 插件提供这些伟大的别名。 它们也可以在狂欢中使用 。

gclean='git clean -fd'
gpristine='git reset --hard && git clean -dfx'

  • gclean 除去未跟踪文件之外未跟踪的目录.
  • gpristine硬重置本地更改, 删除未跟踪的目录, 未跟踪的文件不要使用从.gitignore( 每一个目录) 和 $GIT_ DIR/ info/ excluldude 读取的标准忽略规则, 但是仍然使用给 - e 选项的忽略规则。 这样可以删除所有未跟踪的文件, 包括构建产品。 这可以用来( 可能与 Git 重设结合) 创建一个纯净的工作目录, 测试干净的构建 。.

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

git clean -d -n

它将产生类似的东西:

将删除样本. txt

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

git clean -d -f

它将产生类似的东西:

删除样本. txt

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

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

    git clean -n
    

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

    git clean -fd
    

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