用新的.gitignore文件重新同步git repo [英] Resync git repo with new .gitignore file

查看:94
本文介绍了用新的.gitignore文件重新同步git repo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to "refresh" a git repository after updating the gitignore file?

我刚刚在gitignore中添加了更多的ignorations(?),并希望删除回购中已经与新文件匹配的内容.

I just added more ignorations(?) to my gitignore and would like to remove stuff already in the repo matching the new file.

推荐答案

""中提到的解决方案. gitignore文件不忽略有点极端,但是应该可以工作:

The solution mentioned in ".gitignore file not ignoring" is a bit extreme, but should work:

# 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 下面的评论.
--cached选项将使您的文件保持在磁盘上不变.)

(make sure to commit first your changes you want to keep, to avoid any incident as jball037 comments below.
The --cached option will keep your files untouched on your disk though.)

您还可以在博客文章"让Git忽略已跟踪的文件中找到其他更精细的解决方案

You also have other more fine-grained solution in the blog post "Making Git ignore already-tracked files":

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


Bassim 建议在他的编辑中:

如果出现类似fatal: path spec '...' did not match any files的错误消息,则可能是文件的路径中带有空格.

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.

您可以使用选项--ignore-unmatch删除所有其他文件:

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>"

这篇关于用新的.gitignore文件重新同步git repo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆