GitではGitでコード管理したくないファイルを無視する機能として「.gitignore」が用意されています。
しかし、「.gitignore」でファイルやディレクトリを指定しても、既にGit管理済みのファイルを無視することができません。
このため、git cloneで展開したプロジェクトの中のあるファイルを.gitignoreで管理対象から無視するといったことはできません。
Gitではそんなときのために、専用のコマンドが用意されています。
ここでは、既にGitで管理しているファイルやディレクトリをGitの管理対象から外す方法についてまとめています。
既にGitで管理しているファイルやディレクトリをGitの管理対象から外す方法
目的によってコマンドが異なる
既にGitで管理しているファイルやディレクトリをGitの管理対象から外す方法は、作業ツリーで変更後のファイルをどのように扱うかによって「git update-index」コマンドで使用するオプションが異なります。
- 作業ツリー上の変更を維持する(リモートレポジトリの内容を取り込まない)
- 作業ツリー上の変更を破棄する(リモートレポジトリの内容を取り込む)
| 処理内容 | コマンド |
|---|---|
| 作業ツリー上の変更を維持する | git update-index --skip-worktree <ファイル名> |
| 作業ツリー上の変更を破棄する | git update-index --assume-unchanged <ファイル名> |
作業ツリーとは何か?
作業ツリーとは、git addする前の自分の変更内容のことです。
リモートレポジトリ(Github)
↑ push
ローカルレポジトリ
↑ commit
ステージ(インデックス)
↑ add
作業ツリー(ワークツリー)←ココgit update-indexコマンドとは何か?
git update-indexコマンドは作業ツリーの変更内容を処理するコマンドです。「git add」のように単純にステージに追加する処理もできます。
他にも様々なオプションが用意されています。ここでは「--skip-worktree」と「--assume-unchanged」を使用します。
作業ツリー上の変更を維持する(リモートレポジトリの内容を取り込まない)
コマンド
自分のローカル環境で編集を加えたファイルの変更内容をそのまま保持したい場合は、以下のコマンドを実行します。
$ git update-index --skip-worktree <ファイル名>この指示を解除して、通常どおりにGit管理するように戻したい場合は「no」をつけた「--no-skip-worktree」オプションを使います。
$ git update-index --no-skip-worktree <ファイル名>実例
例えば以下のように、既にGit管理していて変更済みのファイルとして「.htaccess」と「wp-config.php」があるとします。
$ git status
On branch main
Your branch is up to date with 'origin/main'.
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: .htaccess
modified: wp-config.phpこのうち、「.htaccess」に対して、「git update-index --skip-worktree」を実行します。
$ git update-index --skip-worktree .htaccess再度git statusを実施すると、指定したファイルが「modified」から消えます。
$ git status
On branch main
Your branch is up to date with 'origin/main'.
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: wp-config.phpこれで指定したファイルがGit管理から外れました。
除外対象になったファイルの確認
「git update-index --skip-worktree」により、どのファイルが除外対象になっているかを確認するには、「git ls-files -v」コマンドを実行します。
$ git ls-files -vGitで追跡中のファイルはファイル名の冒頭に「H」がつきますが、「git update-index --skip-worktree」を実行したファイルは「S」がつきます。
実例
$ git ls-files -v | grep htaccess
S .htaccess
H wp-content/plugins/akismet/.htaccess
H wp-content/plugins/siteguard/classes/siteguard-htaccess.php除外を元に戻す
除外を元に戻すには「--no-skip-worktree」を実行します。
$ git update-index --no-skip-worktree .htaccess「git status」で状態を確認すると、Git管理から除外していたファイルが復活していることがわかります。
$ git status
On branch main
Your branch is up to date with 'origin/main'.
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: .htaccess
modified: wp-config.php作業ツリー上の変更を破棄する(リモートレポジトリの内容を取り込む)
コマンド
自分のローカル環境で編集を加えたファイルの変更内容を一時的なものとして、git pullやgit rebaseしたときにファイルをリモートレポジトリのもので上書きしたい場合は以下のコマンドを実行します。
$ git update-index --assume-unchanged <ファイル名>この指示を解除して、通常どおりにGit管理するように戻したい場合は「no」をつけた「--no-assume-unchanged」オプションを使います。
$ git update-index --no-assume-unchanged <ファイル名>実例
例えば以下のように、既にGit管理していて変更済みのファイルとして「wp-config.php」があるとします。
$ git status
On branch main
Your branch is up to date with 'origin/main'.
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: wp-config.phpこの「wp-config.php」に対して「git update-index --assume-unchanged」を実行します。
$ git update-index --assume-unchanged wp-config.php再度git statusを実施すると、指定したファイルが「modified」から消えます。
$ git st
On branch main
Your branch is up to date with 'origin/main'.これで指定したファイルがGit管理から外れました。
除外対象になったファイルの確認
「git update-index --assume-unchanged」により、どのファイルが除外対象になっているかを確認するには、「git ls-files -v」コマンドを実行します。
$ git ls-files -vGitで追跡中のファイルはファイル名の冒頭に「H」がつきますが、「git update-index --assume-unchanged」を実行したファイルは「h」(小文字)がつきます。
実例
$ git ls-files -v | grep wp-config
H wp-config-sample.php
h wp-config.php除外を元に戻す
除外を元に戻すには「--no-assume-unchange」を実行します。
$ git update-index --no-assume-unchange wp-config.php「git status」で状態を確認すると、Git管理から除外していたファイルが復活していることがわかります。
$ git status
On branch main
Your branch is up to date with 'origin/main'.
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: wp-config.php

