Git:运行 git clean 时排除一些文件

问题描述 投票:0回答:2

我想清理 .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!

你有什么好主意吗?

git
2个回答
1
投票

问题可以使用命令解决,这有点hacky:

git ls-files --other --ignored --exclude-standard \
  | grep -v -E "^\.env" \
  | xargs -I{} rm -rf {}

0
投票

你可以使用

git clean -dnx -e /.env -e I_do_not_want_add_yet.js
使用双重排除

但是如果文件名是通用的,您将在

node_modules
中有许多文件与之匹配,因此您应该使用完整路径并以
/

开头

git clean -dnx -e /.env -e /path-to-file

© www.soinside.com 2019 - 2024. All rights reserved.