使用新的 .gitignore 文件重新同步 git repo
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7075923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Resync git repo with new .gitignore file
提问by Christian Watteng?rd
Is it possible to "refresh" a git repository after updating the gitignore file?
更新 gitignore 文件后是否可以“刷新”git 存储库?
I just added more ignorations(?) to my gitignore and would like to remove stuff already in the repo matching the new file.
我只是在我的 gitignore 中添加了更多的 ignorations(?) 并且想删除与新文件匹配的 repo 中已有的内容。
回答by VonC
The solution mentioned in ".gitignore file not ignoring" is a bit extreme, but should work:
“ .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"
(make sure to commit first your changes you want to keep, to avoid any incident as jball037comments below.
The --cached
option will keep your files untouched on your disk though.)
(确保首先提交您想要保留的更改,以避免出现以下jball037评论中的任何事件。
尽管该--cached
选项将使您的文件在磁盘上保持不变。)
You also have other more fine-grained solution in the blog post "Making Git ignore already-tracked files":
您在博客文章“使 Git 忽略已跟踪的文件”中还有其他更细粒度的解决方案:
git rm --cached `git ls-files -i --exclude-standard`
Bassimsuggests in his edit:
Files with space in their paths
路径中有空格的文件
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
:
如果您收到类似 的错误消息
fatal: path spec '...' did not match any files
,则可能存在路径中带有空格的文件。您可以使用选项删除所有其他文件
--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>"
回答by gracchus
I might misunderstand, but are you trying to delete files newly ignored or do you want to ignore new modifications to these files ? In this case, the thing is working.
我可能会误解,但是您是要删除新忽略的文件还是要忽略对这些文件的新修改?在这种情况下,事情正在发挥作用。
If you want to delete ignored files previously commited, then use
如果要删除先前提交的忽略文件,请使用
git rm –cached `git ls-files -i –exclude-standard`
git commit -m 'clean up'