博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

gitignore

Posted on 2018-06-16 15:38  bw_0927  阅读(133)  评论(0)    收藏  举报

问题:

在使用git进行版本控制的过程中发现,将想被忽略的文件(文件夹)配置到.gitignore文件中后,实际修改了想被忽略的文件,调用git status查看时,仍然会提示提交这些文件。也就是说实际并没有被忽略

原因:

原因是git ignore只会对不在git仓库中的文件进行忽略,如果这些文件已经在git仓库中,则不会忽略。所以如果需要忽略的文件已经提交到本地仓库,则需要从本地仓库中删除掉,如果已经提交到远端仓库,则需要从远端仓库中删除。删除.gitignore文件才能实际生效。

解决:

  • 从远端仓库clone一份代码
  • 使用git rm file/to/be/ignored -r 删除需要被忽略的文件
  • .gitignore中配置需要被忽略的文件
  • git add . 然后git commit ;再git push 到远端服务器 
    这样保证远端服务器上没有需要被Ignore的文件,即使在本地修改这些文件,使用git status查看也不会再有提示了。

 

** 根本原因 **
.gitignore文件只是ignore没有被staged(cached)文件,对于已经被staged文件,加入ignore文件时一定要从staged移除。下面是来自github的一段话:

If you already have a file checked in, and you want to ignore it, Git will not ignore the file if you add a rule later. In those cases, you must untrack the file first, by running the following command in your terminal:

$ git rm --cached  file_to_ignore

git ls-files --deleted | xargs git rm --cached

git commit