如何从当前工作树中删除未追踪的本地文件?
当前回答
删除未追踪的文件的简单方法
要删除所有未追踪的文件,简单的方式是先添加所有文件并重新设置下面的复制文件。
git add --all
git reset --hard HEAD
其他回答
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 -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 在命令行,只是这样做:
git clean -fd
在做之前双检查,因为它会删除文件和文件夹,而不会创建任何历史。
所以,如果你只想删除文件,你只能使用 -f:
git clean -f
如果您想要删除(目录)和文件,您只能删除未追踪的目录和文件如下:
git clean -fd
并添加 -i 旗帜,使 git 请求您的许可,以便在行程中单独删除文件。
如果您不确定,并希望先检查事物,请添加 -n 旗帜。
使用 -q 如果您不希望在成功删除后看到任何报告。
我也创建下面的图像,使它更令人难忘,特别是我已经看到许多人混淆 -f 为清洁文件夹有时或混合它在某种程度上!
git-clean - 从工作树中删除未追踪的文件 同步 git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x♰ -X] [--] <path>... 描述 通过从当前目录开始,不受版本控制的文件重新删除工作树。
# Print out the list of files and directories which will be removed (dry run)
git clean -n -d
# Delete the files from the repository
git clean -f
注意后两个命令的 X 中的案例差异。
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- 在GitHub上有一个公共回购的私人分支?
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式