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


当前回答

普通普通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 status” 命令中列出的所有更改即可使用以下组合 :

git add -A && git commit -m temp && git reset --hard HEAD^

这将第一阶段 您的所有更改, 然后创建一个临时承诺, 然后丢弃它 。

如果未跟踪的目录是自有的 git 仓库( 如子模块) , 您需要使用-f两次:

git clean -d -f -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 reset HEAD <file>以卸开一个文件