如何从当前工作树中删除未跟踪的本地文件?
#1楼
如果未跟踪的目录是它自己的git存储库(例如,子模块),则需要使用-f
两次:
git clean -d -f -f
#2楼
如果需要从特定子目录中删除未跟踪的文件,
git clean -f {dir_path}
以及删除未跟踪目录/文件和忽略文件的组合方式。
git clean -fxd {dir_path}
之后,您将只能以git status
修改文件。
#3楼
这就是我一直使用的:
git clean -fdx
对于一个非常大的项目,您可能需要运行几次。
#4楼
令我惊讶的是,之前没有人提到过:
git clean -i
这表示交互式 ,您将快速了解要删除的内容,从而可以包含/排除受影响的文件。 总体而言,仍比真正清洁之前运行强制性--dry-run
更快。
如果您还想处理空文件夹,则必须输入-d
。 最后,它提供了一个很好的别名:
git iclean
话虽如此,交互命令的额外手握可能会使有经验的用户感到厌烦。 这些天我只用已经提到过的git clean -fd
#5楼
我喜欢git stash push -u
因为您可以使用git stash pop
撤消它们。
编辑:我也找到了一种方法来显示隐藏的未跟踪文件(例如git show stash@{0}^3
) https://stackoverflow.com/a/12681856/338986
EDIT2:不推荐使用git stash save
而推荐使用push
。 谢谢@ script-wolf。
#6楼
git clean -fd
删除目录
git clean -fX
删除忽略的文件
git clean -fx
删除被忽略和未被忽略的文件
可以结合使用以上所有选项
git clean -fdXx
检查git手册以获得更多帮助
#7楼
更好的方法是使用:git clean
git clean -d -x -f
这将删除未跟踪的文件,包括目录(-d)
和git (-x)
忽略的文件。
另外,将-f
参数替换为-n
以执行dry-run
或者将-i
替换为交互式模式,它将告诉您要删除的内容。
#8楼
git clean -f -d -x $(git rev-parse --show-cdup)
将clean应用于根目录,无论您在存储库目录树中的何处调用它。 我一直在使用它,因为它不会强迫您离开您现在使用的文件夹,并且可以从您所在的位置直接进行清理和提交。
确保标志-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 s