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


当前回答

我们可以轻松地从当前 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

其他回答

更好的方法是使用: git clean

git clean -d -x -f

这将删除未追踪的文件,包括目录(d)和由 git(x)忽略的文件。

此外,将 -f 论点替换为 -n 以进行干燥运行或 -i 为互动模式,它会告诉你什么将被删除。

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 stash 

更多信息 https://www.atlassian.com/git/tutorials/saving-changes/git-stash#stashing-your-work

git-clean 是你正在寻找的,它被用来从工作树中删除未追踪的文件。

我用这个:

git 状态 复制文件路径 rm <文件路径>

我的项目有很多由一个巨大的 ANT 构建脚本创建的创建文件,使用 git clean 会产生混乱。