LoginSignup
664
596

More than 5 years have passed since last update.

あとからまとめて.gitignoreする方法

Last updated at Posted at 2014-06-20

.gitignore を設置

GitHub に .gitignore のテンプレートがあるのでそれを使うとらくです。

.gitignore をコミット

チームで開発していたり、複数のマシンで編集する可能性がある場合は、他の環境でも同様にこれらのファイルを無視してくれるように、.gitignore 自体をコミットしてしまいましょう。

$ git add .gitignore
$ git commit -m "Add ignore pattern"

無視したいファイルをリポジトリから削除

.gitignore を設置しても、既にリポジトリに登録されているファイルは無視されないので、無視したいファイルを管理対象から外します。

$ git rm --cached hoge.tmp

(なお、管理対象から外れるだけで、ローカルにあるファイルは削除されません)

ただ、既に大量のファイルが登録されている場合、これをひとつひとつやっていたら大変なので、以下のように git ls-files-i--exclude-from オプションを使って、まとめて除外するとらくです。

$ git rm --cached `git ls-files --full-name -i --exclude-from=.gitignore`
  • -i--ignored の略で、Git に登録されているファイルの内、次に指定する方法で無視するファイルの一覧を出力します。
  • --exclude-from=<ignore-file> を指定すると、<ignore-file> に記述されたパターンにマッチするファイルを、無視するファイルとして扱います。
  • --exclude-standard を指定すると、Git が標準で無視リストとして扱う .gitignore.git/info/exclude などのファイルに書かれたパターンにマッチするファイルを、無視するファイルとして扱います。

--exclude-standard を使う場合は、以下のように入力します。

$ git rm --cached `git ls-files --full-name -i --exclude-standard`

最後に、確認して問題なければコミットしましょう。

$ git status
$ git commit -m "Remove ignore files"

追記

  • 2014/07/07 まとめて除外するコマンドを修正しました。
  • 2014/10/10 説明をわかりやすくなるように編集しました。
664
596
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
664
596