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


当前回答

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 add .
git reset --hard HEAD

注意! 请确保在执行此事之前进行任何必要的更改(即使在未被拖动的文件中)。

如果没有其他工作,可以使用下列组合来简单地删除“吉特状态”命令列出的所有变化:

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

这将首先完成您的所有变化,然后创建一个暂时的承诺,然后放弃它。

git clean -f 从工作目录中删除未追踪的文件。

我在我的博客中包含了一些基本内容, git-intro-basic-commands

git clean -fd 删除目录

git clean -fX 删除被忽略的文件

git clean -fx 删除被忽略和未被忽略的文件

可以使用上述所有选项,如

清洁 -fdXx

查看 git 手册 获取更多帮助