我想清理 .gitignore 忽略的文件,但我想排除一些由排除选项指定的文件。 然后,我不想删除未跟踪的文件。
dist (ignored)
node_modules (ignored)
.env (ignored but I want to exclude for the cleanup)
I_do_not_want_add_yet.js (untracked, I don't want cleanup some untracked files)
package.json (There are many other tracked files)
所以我查看了一些帖子并尝试了以下命令:
$ git clean -ndX -e .env
Would remove dist/
Would remove node_modules/
Would remove .env # Oops!
$ git clean -ndX --exclude='!.env'
Would remove dist/
Would remove node_modules/
Would remove .env # Oops!
$ git clean -ndx -e .env
Would remove dist/
Would remove node_modules/
Would remove I_do_not_want_add_yet.js # Oops!
你有什么好主意吗?
问题可以使用命令解决,这有点hacky:
git ls-files --other --ignored --exclude-standard \
| grep -v -E "^\.env" \
| xargs -I{} rm -rf {}
你可以使用
git clean -dnx -e /.env -e I_do_not_want_add_yet.js
使用双重排除
但是如果文件名是通用的,您将在
node_modules
中有许多文件与之匹配,因此您应该使用完整路径并以 /
开头
git clean -dnx -e /.env -e /path-to-file