在项目中,一开始有某个文件(如: .DS_Store)被git跟踪提交过,但后续我想使git不在跟踪这个文件。可当我在.gitignore中配置这个文件之后,git还是会跟踪这个文件的变化。
# 这条命令会列出所有被 git 跟踪的 .DS_Store 文件。
git ls-files | grep .DS_Store
git rm --cached 路径/到/.DS_Store
也可以批量处理
git ls-files | grep .DS_Store | xargs git rm --cached
git commit -m "remove: tracked .DS_Store files"
这样就不会再有 .DS_Store 被 git 监听到变化了。之后新生成的 .DS_Store 会被 .gitignore 忽略,不再进入 git 跟踪。