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


当前回答

要知道在实际删除之前会删除什么:

清洁 -d -n

它会产生这样的东西:

将移除 sample.txt

要删除前命令输出中列出的所有内容:

清洁 -d -f

它会产生这样的东西:

删除 sample.txt

其他回答

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

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

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

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 clean -f {dir_path}

并结合的方式来删除未追踪的Dir/文件和被忽略的文件。

git clean -fxd {dir_path}

此后,您将仅在 git 状态中修改文件。

要知道在实际删除之前会删除什么:

清洁 -d -n

它会产生这样的东西:

将移除 sample.txt

要删除前命令输出中列出的所有内容:

清洁 -d -f

它会产生这样的东西:

删除 sample.txt

我们可以轻松地从当前 git 工作树中删除本地未追踪的文件,使用下面的 git 评论。

git reset [--soft | --mixed [-N] | --hard | --merge | --keep] [-q] [<commit>]

例子:

git reset --hard HEAD

左:

https://git-scm.com/docs/git-reset 如何使用‘git reset --hard HEAD’ 返回以前的承诺? 恢复本地存储分支 就像远程存储分支 HEAD https://jwiegley.github.io/git-from-the-bottom-up/3-Reset/4-doing-a-hard-reset.html