madokaのブログ

勉強したことのoutput先として使ってます。内容はpythonがらみが多いかもです。

.gitignoreを変更せずに、ファイルを無視したい

動機

この度、intelliJのUltimate版を購入したのがきっかけで、.gitignoreファイルに.ideaを追記したくなったのが事の発端。 しかし、自分の事情だけでファイルを変更するのは、あまり好ましくありません。 ということで、他の方法で対象から外す方法を調査しました。

あった

git cloneや、git initをしたときにできる .git ディレクトリの中にありました。 .git/info/excludeファイル(repositoryからの相対path) です。ここに、.gitignoreと同じように記述することで、そのファイルらを対象から外すことができました。

before

$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .idea/
    my_test.iml

nothing added to commit but untracked files present (use "git add" to track)

さっそく追記します

vimで開いて編集しました。

# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
.idea  #追記
*.iml   #追記

after

$ git status
On branch master
nothing to commit, working directory clean

みごと、gitから.idea/と.imlファイルが無視されるようになりました。

参考させていただいたページ

https://qiita.com/kiyodori/items/a29f7be2013803250141