.gitignoreで無視されているファイルを一時的にコミットしたい

.gitignoreで無視しているファイルを、デバッグ用途などで一時的にコミットしたい場合がある。通常git addで無視対象のファイルを指定すると、エラーになりステージングされない。

$ cat .gitignore
*.log

$ git add app.log
The following paths are ignored by one of your .gitignore files:
app.log
hint: Use -f if you really want to add them.

-f--force)オプションを付けると、.gitignoreの設定を無視してファイルを強制的にステージングできる。

$ git add -f app.log
$ git status --short
A  app.log

無視されたディレクトリごと追加する

ディレクトリを指定した場合も同様に、-fを付けると配下の無視されたファイルをまとめて強制的にステージングできる。

$ cat .gitignore
build/

$ git add -f build/
$ git status --short
A  build/app1
A  build/app2

ファイルが無視されているか事前に確認する

意図せず.gitignoreに該当しているファイルなのか判断が付かない場合は、git add -fを実行する前にgit check-ignoreコマンドで確認するとよい。対象ファイルがどの.gitignoreのルールで無視されているかを確認できる。詳細は【git check-ignore】ファイルが.gitignoreで無視されているか確認する を参照。

注意点

git add -f.gitignoreの意図に反してファイルをコミットしてしまう操作である。認証情報やビルド成果物など、本来リポジトリに含めるべきでないファイルを誤ってコミットしないよう注意が必要である。

git-add - Git Documentation