LoginSignup
8
7

More than 3 years have passed since last update.

gitignoreが無視される原因と対策

Posted at

背景

Python勉強中。
学習用コードのバージョン管理にgitを使おうと、Visual Studio Code上のターミナル(PowerShell)にて、
gitignore.ioを利用して.gitignoreファイルを作成した。

PowerShell
git ignore Python > .gitignore

しかし こうかが なかった。
なぜか、キャッシュファイルであるはずの*.pyc が平然とステージ待ちしている。
.gitignore ファイルでは除外指定をしているはず。

.gitignore
### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

#以下略

おかしい。

環境

  • Windows 10
  • Visual Studio Code 1.38.1
  • git 2.13.2.windows.1

原因と対策

すでにgit管理になっている

ググるとよくこれが出てくる。
対象ファイルが既にgit管理対象になっているケース。
キャッシュクリアすることで解決するらしい。

$ git rm -r --cached .

しかし、今回は一度もコミットしておらず、そもそも「さあこれから管理しよう」という段階での問題…。

.gitignoreの文字コードがUTF-16

今回のケースはこれだった。
どうやらPowerShellでリダイレクトして作成したテキストファイルは文字コードがUnicode(UTF-16LE)になるらしく、gitが認識してくれなかった模様。
VSCodeで一度開き、UTF-8として保存し直すことで、正しく認識してくれるようになった。

参考ページ

.gitignore 設定が反映されない場合の対応

8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7