0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

グローバルなGitの除外ファイル

Last updated at Posted at 2025-10-02

よく .gitignore_global を使っていたのですがスタンダードな方法が変わったようです。毎回忘れて調べるので自分用にメモします。

TL;DR

# 今まで

echo foobar > ~/.gitignore_global
git config --global core.excludesfile ~/.gitignore_global
# これから

echo foobar > ~/.config/git/ignore

なぜ?

もともと core.excludesfile に任意のファイルを登録すれば良いらしく ".gitignore_global" というファイル名が慣習的に使われている、ということです。PC を替えるといそいそとファイル作ってましたが別に何でも良かったんですね。

さらに git のバージョンアップにより core.excludesfile にわざわざ登録しなくても良くなりました。$XDG_CONFIG_HOME/git/ignore.gitignore_global に変わるスタンダードです。

https://git-scm.com/docs/gitignore

Patterns which a user wants Git to ignore in all situations (e.g., backup or temporary files generated by the user’s editor of choice) generally go into a file specified by core.excludesFile in the user’s ~/.gitconfig. Its default value is $XDG_CONFIG_HOME/git/ignore. If $XDG_CONFIG_HOME is either not set or empty, $HOME/.config/git/ignore is used instead.

Git は「XDG Base Directory」に準拠しています。これは設定ファイルやデータなど種類ごとのベースディレクトリを定めるものです。

https://specifications.freedesktop.org/basedir-spec/latest/

~/.config/ # 設定ファイル
~/.cache/ # キャッシュ
~/.local/share/ # 一時ファイル

ベースディレクトリは環境変数で変更できます。$XDG_CONFIG_HOME は設定ファイルのディレクトリの場所を示します。

printenv |grep XDG_CONFIG_HOME
> 

git の説明では環境変数が空ならデフォルトの ~/.config/git/ignore が適用されると書かれています。

touch foobarbaz.json

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

# 除外ファイルに追加
echo foobarbaz.json >> ~/.config/git/ignore

git status
> ...
> nothing to commit, working tree clean

...おお、ちゃんと効いてますね!

  • Git の除外ファイルは $XDG_CONFIG_HOME/git/ignore に書く
  • デフォルトだから core.excludesfile しなくて良い

まとめるとこういう事みたいです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?