如何从当前工作树中删除未追踪的本地文件?
当前回答
git add --all, git stash and git stash drop, try these three commands in this order in order to remove all untracked files. 通过添加所有这些未追踪的文件到 git 并 stashing 它们将移动所有这些未追踪的文件到 stash 列表并下载顶部一个,即, stash@{0} 将从 stash 列表中删除所有未追踪的文件。
其他回答
git clean -fd 删除目录
git clean -fX 删除被忽略的文件
git clean -fx 删除被忽略和未被忽略的文件
可以使用上述所有选项,如
清洁 -fdXx
查看 git 手册 获取更多帮助
这是我一直使用的:
git clean -fdx
对于一个非常大的项目,你可能想运行它几次。
命令从 git docs 删除 Untracked 文件是 git clean
git clean - 从工作树中删除未追踪的文件
可用的选项:
git clean
-d -f -i -n -q -e -x -X (can use either)
解释:
1 - D
除未追踪的目录外,除未追踪的文件外,除未追踪的目录外,除未追踪的目录外,除非由另一个 Git 存储库管理,则不会默认删除。
如果 Git 配置变量 clean.requireForce 未设置为虚假,则 git clean 将拒绝运行,除非提供 -f、 -n 或 -i。
3 -i, - 互动
4 -n, -dry-run
5 -q, - 放心
保持沉默,只报告错误,但不是成功删除的文件。
除了在.gitignore (每个目录) 和 $GIT_DIR/info/exclude 中找到的外,还考虑这些模式在无效的规则中。
7、X
8、X
删除仅由 Git 忽略的文件,这可能有助于重建一切,但保持手动创建的文件。
删除未追踪的文件的简单方法
要删除所有未追踪的文件,简单的方式是先添加所有文件并重新设置下面的复制文件。
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添加?
- 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日志日期格式