更新gitignore文件后是否可以“刷新”git存储库?
我刚刚向我的gitignore添加了更多的ignorations(?),并希望删除与新文件匹配的repo中的内容。
答案 0 :(得分:328)
“.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`
如果收到
fatal: path spec '...' did not match any files
之类的错误消息,可能是路径中有空格的文件。您可以使用选项
--ignore-unmatch
删除所有其他文件:
git rm --cached --ignore-unmatch `git ls-files -i --exclude-standard`
但是不匹配的文件将保留在您的存储库中,并且必须通过用双引号括起其路径来明确删除它们:
git rm --cached "<path.to.remaining.file>"
答案 1 :(得分:9)
我可能会误解,但您是在尝试删除新忽略的文件,还是要忽略对这些文件的新修改?在这种情况下,事情正在发挥作用。
如果要删除先前提交的被忽略文件,请使用
git rm –cached `git ls-files -i –exclude-standard`
git commit -m 'clean up'
答案 2 :(得分:1)
我知道这是一个老问题,但是如果文件名包含空格,gracchus的解决方案将不起作用。 VonC解决方案中带空格的文件名的方法是不使用--ignore-unmatch
删除它们,然后手动删除它们,但是如果数量很多,这种方法将无法正常工作。
这是一种利用bash数组捕获所有文件的解决方案。
# Build bash array of the file names
while read -r file; do
rmlist+=( "$file" )
done < <(git ls-files -i --exclude-standard)
git rm –-cached "${rmlist[@]}"
git commit -m 'ignore update'