如何从当前工作树中删除未追踪的本地文件?
当前回答
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 存储库和所有子模块重复 以下命令将清除当前 git 存储库和所有子模块重复: (git clean -d -x -f& git submodule foreach --recursive git clean -d -x -f)
我很惊讶以前没有人提到这一点:
git clean -i
它是互动的,你会得到一个快速的概述,什么将被删除,为您提供包含 / 排除受影响的文件的可能性。
你必須在 -d 如果你也想照顧空的文件夾。
git iclean
说到这一点,对互动命令的额外持有可能令人兴奋的经验丰富的用户,如今我只使用已经提到的 git clean -fd
如果未追踪的目录是自己的 git 存储库(例如子模块),您需要使用 -f 两次:
清洁 -d -f -f
使用 git clean -f -d 确保目录也被删除。
实际上不要删除任何东西,只需显示会发生什么。 git clean -n 或 git clean --dry-run 除去不跟踪的目录除了不跟踪的文件. 如果一个不跟踪的目录由另一个 Git 存储库管理,它不会默认删除。
然后,您可以检查您的文件是否真的失去了 git 状态。
用户互动方法:
git clean -i -fd
Remove .classpath [y/N]? N
Remove .gitignore [y/N]? N
Remove .project [y/N]? N
Remove .settings/ [y/N]? N
Remove src/com/arsdumpgenerator/inspector/ [y/N]? y
Remove src/com/arsdumpgenerator/manifest/ [y/N]? y
Remove src/com/arsdumpgenerator/s3/ [y/N]? y
Remove tst/com/arsdumpgenerator/manifest/ [y/N]? y
Remove tst/com/arsdumpgenerator/s3/ [y/N]? y
-i for interactive -f for force -d for directory -x for ignored files(添加必要时) 注意: 添加 -n 或 -dry-run 只需检查它会做什么。
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的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日志日期格式