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


当前回答

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 stash save -u
git stash drop "stash@{0}"

我更喜欢“清洁”这个,因为“清洁”会删除被Git忽略的文件,所以你的下一个构建将不得不重建一切,你可能会失去你的IDE设置。

git clean -f

将从当前 git 中删除未追踪的文件

git clean -fd

当您要删除目录和文件时,此只会删除未追踪的目录和文件。

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

实际上不要删除任何东西,只需显示会发生什么。 git clean -n 或 git clean --dry-run 除去不跟踪的目录除了不跟踪的文件. 如果一个不跟踪的目录由另一个 Git 存储库管理,它不会默认删除。

然后,您可以检查您的文件是否真的失去了 git 状态。

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

这将使你处于新鲜的克隆状态。

git clean -ffdx

在此 repo 中删除所有额外的文件夹和文件,但不是其子模块

git clean -fdx

删除额外的文件夹,但不删除文件(例如构建或登录文件夹)

git clean -fd

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

如果文件没有被忽视,并且还没有被检查,那么它仍然存在。

git clean -fdX

新互动模式

git clean

删除未追踪的文件的简单方法

要删除所有未追踪的文件,简单的方式是先添加所有文件并重新设置下面的复制文件。

git add --all
git reset --hard HEAD