.gitignoreに記載したのに、diffに無視したいファイルが出てくる!なんで!ってことありますよね。多くの人が1度は経験するんじゃないかと思います。
そんなときに、.gitignoreに記載したのに、反映されないという問題の原因と解決方法を徹底解説していきたいと思います。
原因
「無視したいファイルがすでにリモートブランチにあがっている」
.gitignoreに記載したにもかかわらず反映されない、一番の原因はこれだと思います。
例えば下記の例だと、log/development.logを.gitignoreに記載しているにもかかわらず、git diffにはlog/development.logが存在しています。
% cat .gitignore tmp/* log/*
% git status On branch master Your branch is ahead of 'origin/master' by 2 commits. (use "git push" to publish your local commits) Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: .gitignore modified: log/development.log
このときのリモートブランチの状態を確認すると過去時点の、development.logがすでにコミットされています。
解決策 キャッシュの消去
解決策は、git管理しないファイルであれば、git管理から外してしまうのが最も早いです。
例えば先ほどのファイルの場合をgit管理対象外にし、.gitignoreの設定が反映されているか確認したいと思います。
% git rm --cached log/* rm 'log/development.log' rm 'log/production.log'
ここでstatusを見ると下記のようになっていますが、コミット、プッシュしても、ローカルのファイルは消去されないので、安心してください。
% git status On branch master Your branch is ahead of 'origin/master' by 3 commits. (use "git push" to publish your local commits) Changes to be committed: (use "git reset HEAD <file>..." to unstage) deleted: log/development.log deleted: log/production.log
コミットしてプッシュしちゃいます。
% git commit -m 'remove log files from git management' [master 096380f] remove log files from git management 2 files changed, 1670 deletions(-) delete mode 100644 log/development.log delete mode 100644 log/production.log % git push origin master
当然リモートブランチからは、logファイルが消えています。
これでgit statusをすると、先ほどみたいにlogファイルはでなくなります。
% git status On branch master Your branch is up-to-date with 'origin/master'. nothing to commit, working directory clean
これで、.gitignoreに記載しても反映されないという誰もが一度は経験するであろう問題の解決策は以上です。困っているどなたかの参考になれば幸いです。