git - 使用新的 .gitignore 文件重新同步 git repo

标签 git

是否可以在更新 gitignore 文件后“刷新”git 存储库?

我刚刚向我的 gitignore 添加了更多的忽略(?),并想删除与新文件匹配的存储库中已有的东西。

最佳答案

.gitignore file not ignoring”中提到的解决方案有点极端,但应该可行:

# rm all files
git rm -r --cached .
# add all files as per new .gitignore
git add .
# now, commit for new .gitignore to apply
git commit -m ".gitignore is now working"

(确保首先提交您想要保留的更改,以避免发生任何 jball037 comments below 的事件。
--cached 选项将使您的文件在磁盘上保持不变。)

您在博文“Making Git ignore already-tracked files”中还有其他更细粒度的解决方案:

git rm --cached `git ls-files -i --exclude-standard`

Bassim建议 in his edit :

路径中有空格的文件

In case you get an error message like fatal: path spec '...' did not match any files, there might be files with spaces in their path.

You can remove all other files with option --ignore-unmatch:

git rm --cached --ignore-unmatch `git ls-files -i --exclude-standard`

but unmatched files will remain in your repository and will have to be removed explicitly by enclosing their path with double quotes:

git rm --cached "<path.to.remaining.file>"

关于git - 使用新的 .gitignore 文件重新同步 git repo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7075923/

相关文章:

php - 强制 composer.json 导出供应商存储库而不是克隆

git - 在 Gitlab 中用于分支的 Webhooks

git - Heroku 如何只允许我的 git 帐户推送到其创建的 git 远程服务器?

java - 如果有未提交的更改,Maven 会阻止构建

ruby-on-rails - 在本地主机上设置 Rails 项目,postgresql 问题

git - 创建远程主机的分支

git:如何将分支的根两个提交移回

git - git 子模块可以有自定义根吗?

git - 如何在 git 上恢复旧提交中的特定文件

python - 如何解决Python Git子模块的依赖关系