LoginSignup
351
315

More than 5 years have passed since last update.

git の監視から逃れる方法

Posted at

135661733965913209789.png

既に追跡されている

追跡の必要性がなくなった場合

ファイルを削除 & インデックスから削除して commit

git rm path/to/file
git commit -m 'Good-bye file'

なお, ファイルを残したまま追跡から逃れたいときは git rm --cached path/to/file でインデックスのみ削除し, .gitignore などで指定する. また, 誤って git rm してしまったときは git reset @~ path/to/file, git checkout path/to/file で回復する.

ローカルでのみ追跡してほしくない場合

二つ方法がある. これらの違いは既に git 管理しているファイルをあえて無視したい - Qiitaに詳しい.
用途に依るが, assume-unchanged の変更は git reset --hard で死ぬので, 基本的には skip-worktree を使う.

assume-unchanged

除外するとき

git update-index --assume-unchanged path/to/file

除外から戻すとき

git update-index --no-assume-unchanged path/to/file

確認するとき

git ls-files -v | grep ^h

skip-worktree

除外するとき

git update-index --skip-worktree path/to/file

除外から戻すとき

git update-index --no-skip-worktree path/to/file

確認するとき

git ls-files -v | grep ^S

まだ追跡されていない

プロジェクト単位で追跡してほしくない場合

.gitignore に記述する.

ローカルでのみ追跡してほしくない

対象リポジトリのみで追跡から逃れる場合

.git/info/exclude に記述する.

ローカルの全リポジトリで追跡から逃れる場合

ホームディレクトリに .gitignore を用意する (今回は区別のため .gitignore_global とした.)

~/.gitignore_global
*~
*.swp
*.swo
.DS_Store
*~
*#
.#*

git の global 設定に保存する

git config --global core.excludesfile $HOME/.gitignore_global

上のコマンドにより, 以下のように設定が ~/.gitconfig に反映される.

~/.gitconfig
[core]
    excludesfile = /home/{ユーザー名}/.gitignore_global

References

351
315
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
351
315