2
2

More than 1 year has passed since last update.

既にGitで追跡済みのファイルはgitignoreで除外されない話

Last updated at Posted at 2022-07-30

はじめに

Git管理対象外にしたいファイルやディレクトリを .gitignore に記載することで実現することができます。
しかし、先日 .gitignore に指定してもGit管理対象外とならなずに躓いたため、その共有です。

結論

何が原因でGit管理対象外とならなかったかというと、既にコミット(またはステージング)済みのファイルを管理対象外としようとしていたためでした。

少し考えたら当然ですが、既にGitで追跡済みのファイルに対しては .gitignore に指定しても効力がないらしいです。

gitのドキュメントに以下のような記載がありました。

A gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected; see the NOTES below for details.

実験

実際に手元で挙動を色々確認してみます。

次の3種類の状態のファイルを .gitignore に指定した時の振る舞いをみていきます。

> tree .
.
├── added.txt      // ステージング状態だが、まだ一度もコミットしていないファイル
├── committed.txt  // 既にコミット済みのファイル
└── not_added.txt  // 一度もコミットもステージング状態にもしていないファイル
> git status
On branch gitignore-demo
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   added.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        not_added.txt

これらファイルを .gitignore に追加します。

.gitignore
added.txt
committed.txt
not_added.txt

この状態で git status で状態を見ると以下となりました。

> git status
On branch gitignore-demo
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   added.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   .gitignore

not_added.txt は管理対象外となりましたが、一度ステージングした added.txt はしっかり管理下にいます。

git reset HEAD でステージングされている added.txt をステージング前の状態に戻すとどうなるか試してみます。

> git reset HEAD
Unstaged changes after reset:
M       .gitignore
> git status
On branch gitignore-demo
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   .gitignore

no changes added to commit (use "git add" and/or "git commit -a")

きちんと管理対象外になりました。

次に一度コミットされている committed.txt を編集して、状態を確認してみます。

修正後のgit status
> git status
On branch gitignore-demo
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   .gitignore
        modified:   committed.txt

no changes added to commit (use "git add" and/or "git commit -a")

一度Gitで追跡しているので、 .gitignore で指定していても除外されません。

既に追跡済みのファイルを除外する

先ほどのような、既にGitで追跡しているファイルをGit管理対象外にしたい場合は以下コマンドを実行して、キャッシュを削除します。

> git rm --cached committed.txt

キャッシュ削除後の状態は以下となります。

> git status
On branch gitignore-demo
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    committed.txt

no changes added to commit (use "git add" and/or "git commit -a")

この変更をコミットすれば committed.txt は管理対象外となりました。

参考

2
2
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
2
2