English

Gitの設定がどのファイルに書かれているかを調べる方法

​​Git​コマンドライン

git config --show-origin を使うと、いま効いている Git の設定が、どの設定ファイルに書かれているかが分かります。git pull の動きやコミットの作者が思ったとおりにならないときに、「設定したはずの値が効かない」「覚えのない設定が効いている」原因のファイルを突き止める方法です。--get-all を付ければ、上書きされて隠れている値も、書かれたファイルと一緒に並べて出せます。

補足

検証環境は Ubuntu 24.04、Git 2.43.0 です。名前とメールアドレスは例示用の値(Neko <neko@example.com>)にし、ホームフォルダは、何もない状態の /home/user を用意して使いました。システム全体の設定ファイル /etc/gitconfig には、検証用に pull.rebase = false と core.autocrlf = input だけを書いたファイルを使っています。Windows・macOS での表示や、Git for Windows に付いてくる設定ファイルは試していません。

すべての設定を、書かれたファイルと一緒に出す

検証では、全体の設定(--global)に名前・メールアドレスと pull.rebase = true を入れ、リポジトリ cat-tools を作りました。その中で git config --list --show-origin を実行すると、1行に1つずつ、設定が書かれたファイルと値が並びます。

console
$ git config --list --show-origin
file:/etc/gitconfig	pull.rebase=false
file:/etc/gitconfig	core.autocrlf=input
file:/home/user/.gitconfig	user.name=Neko
file:/home/user/.gitconfig	user.email=neko@example.com
file:/home/user/.gitconfig	init.defaultbranch=main
file:/home/user/.gitconfig	pull.rebase=true
file:.git/config	core.repositoryformatversion=0
file:.git/config	core.filemode=true
file:.git/config	core.bare=false
file:.git/config	core.logallrefupdates=true

左の file: の後ろが、その設定が書かれたファイルです。/etc/gitconfig はシステム全体の設定、/home/user/.gitconfig はユーザーの設定(全体の設定)、.git/config はこのリポジトリの設定です。リポジトリの設定は、.git/config という相対パスで表示されました。

--show-scope も一緒に付けると、ファイル名の前に、どの種類の設定なのか(system、global、local)も出ます。--show-scope は、「Gitの名前とメールアドレスをリポジトリごとに変える方法」の記事でも使いました。

console
$ git config --list --show-origin --show-scope
system	file:/etc/gitconfig	pull.rebase=false
system	file:/etc/gitconfig	core.autocrlf=input
global	file:/home/user/.gitconfig	user.name=Neko
global	file:/home/user/.gitconfig	user.email=neko@example.com
global	file:/home/user/.gitconfig	init.defaultbranch=main
global	file:/home/user/.gitconfig	pull.rebase=true
local	file:.git/config	core.repositoryformatversion=0
local	file:.git/config	core.filemode=true
local	file:.git/config	core.bare=false
local	file:.git/config	core.logallrefupdates=true

git config の公式リファレンスによると、--show-origin は値の出どころの種類(ファイル、標準入力、blob、コマンドライン)と実際の出どころ(ファイルのパスなど)を、--show-scope は値のスコープ(worktree、local、global、system、command)を出力に足すオプションです。

1つの設定が、どのファイルから効いているかを見る

--list の一覧では、pull.rebase が /etc/gitconfig と ~/.gitconfig の2か所に書かれています。実際にどちらが効いているかは、設定の名前を指定して --show-origin を付けると分かります。

console
$ git config pull.rebase
true

$ git config --show-origin pull.rebase
file:/home/user/.gitconfig	true

効いているのは ~/.gitconfig の true でした。--get-all を付けると、同じ名前の設定を、読んだ順にすべて出せます。

console
$ git config --show-origin --get-all pull.rebase
file:/etc/gitconfig	false
file:/home/user/.gitconfig	true

公式リファレンスの FILES の節によると、Git はシステム全体の設定、全体の設定、リポジトリの設定の順にファイルを読み、後から見つかった値を優先します。--get-all の出力でいちばん下にある値が、いま効いている値です。

リポジトリの設定を足した後

cat-tools の中で、--global を付けずに pull.rebase を false にしました。操作の後に同じコマンドを実行すると、効いている値の出どころが .git/config に変わりました。

console
$ git config pull.rebase false

$ git config pull.rebase
false

$ git config --show-origin pull.rebase
file:.git/config	false

$ git config --show-origin --get-all pull.rebase
file:/etc/gitconfig	false
file:/home/user/.gitconfig	true
file:.git/config	false

~/.gitconfig の true は消えたわけではなく、後から読んだ .git/config の値に上書きされています。リポジトリの外(ホームフォルダ)で同じコマンドを実行すると、.git/config は読まれず、2行だけになりました。

console
$ git config --show-origin --get-all pull.rebase
file:/etc/gitconfig	false
file:/home/user/.gitconfig	true

読み込んだ別のファイルの設定も、そのファイル名で出る

~/.gitconfig に [include] を書いて別のファイルを読み込んでいると、~/.gitconfig を開いても設定が見つかりません。検証では、ホームフォルダでエイリアスを ~/.gitconfig-alias に書き、全体の設定から読み込みました。

console
$ printf "[alias]\n\tst = status --short\n\tlg = log --oneline\n" > .gitconfig-alias && git config --global include.path "~/.gitconfig-alias"

$ cat .gitconfig
[user]
	name = Neko
	email = neko@example.com
[init]
	defaultBranch = main
[pull]
	rebase = true
[include]
	path = ~/.gitconfig-alias

--show-origin を付けると、エイリアスは読み込まれた側のファイル名で表示されました。

console
$ git config --show-origin --get-regexp "^alias\."
file:/home/user/.gitconfig-alias	alias.st status --short
file:/home/user/.gitconfig-alias	alias.lg log --oneline

--show-scope だけでは、この値は global としか分かりません。両方を付けると、全体の設定のうち、どのファイルに書かれているかまで出ます。

console
$ git config --show-origin --show-scope alias.st
global	file:/home/user/.gitconfig-alias	status --short

[include] の path は、include.path という設定として ~/.gitconfig 側に出ます。include も一緒に検索すると、どのファイルからどのファイルを読み込んでいるかを、まとめて確かめられます。

console
$ git config --show-origin --get-regexp "^(alias|include)\."
file:/home/user/.gitconfig	include.path ~/.gitconfig-alias
file:/home/user/.gitconfig-alias	alias.st status --short
file:/home/user/.gitconfig-alias	alias.lg log --oneline

全体の設定ファイルは、~/.gitconfig のほかに ~/.config/git/config もあります(公式リファレンスの FILES の節)。~/.config/git/config に core.editor = vim を書き、~/.gitconfig に core.editor = nano を足すと、どちらも global で、ファイル名で見分けられました。後から読む ~/.gitconfig の nano が使われます。

console
$ git config --show-origin --show-scope --get-all core.editor
global	file:/home/user/.config/git/config	vim
global	file:/home/user/.gitconfig	nano

$ git config core.editor
nano

どのファイルにもない値は「command line:」と出る

git -c で渡した設定は、ファイルではなく command line: と表示されました。

console
$ git -c pull.rebase=merges config --show-origin pull.rebase
command line:	merges

環境変数 GIT_CONFIG_COUNT、GIT_CONFIG_KEY_<n>、GIT_CONFIG_VALUE_<n> で渡した設定も、同じく command line: になり、スコープは command でした。

console
$ GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=pull.rebase GIT_CONFIG_VALUE_0=merges git config --show-origin --show-scope --get-all pull.rebase
system	file:/etc/gitconfig	false
global	file:/home/user/.gitconfig	true
local	file:.git/config	false
command	command line:	merges

公式リファレンスの ENVIRONMENT の節によると、これらの環境変数で渡した設定は設定ファイルの値より優先され、git -c で渡した値はさらにそれより優先されます。どの設定ファイルにも見当たらない値が効いているときは、command line: の行がないかを見て、GIT_CONFIG_COUNT などの環境変数が設定されていないか、エイリアスやスクリプトの中で git -c を使っていないかを調べます。

pull.rebase を、システム全体の設定、全体の設定、リポジトリの設定、コマンドラインの順に読み、最後の merges が使われることを示す図
設定ファイルを読む順番と、いま効いている値

見つけたファイルから設定を消す

出どころのファイルが分かったら、--file でそのファイルを指定して消せます。公式リファレンスでは、--file は書き込むときにそのファイルへ書き、読むときはそのファイルだけを読むオプションです。ここでは、~/.gitconfig の pull.rebase を消しました。

console
$ git config --file ~/.gitconfig --unset pull.rebase

$ git config --show-origin --get-all pull.rebase
file:/etc/gitconfig	false
file:.git/config	false

続けて .git/config の pull.rebase も消すと、残るのはシステム全体の設定だけになり、その値が効きました。

console
$ git config --file .git/config --unset pull.rebase

$ git config --show-origin --get-all pull.rebase
file:/etc/gitconfig	false

$ git config pull.rebase
false
注意

--file も --global も付けずに git config --unset を実行すると、リポジトリの .git/config だけが対象になります。公式リファレンスにあるとおり、git config が一度に変えるファイルは1つだけです。全体の設定やインクルードしたファイルにある値は、--show-origin で出たファイルを --file で指定して消してください。

どのファイルにも書かれていない名前を指定すると、何も表示されませんでした。

console
$ git config --show-origin no.such

このときの終了コードは 1 でした。公式リファレンスにも、名前が見つからないときは終了コード 1 を返すと書かれています。何も出ないときは、名前の打ち間違いか、その設定がどこにもないかのどちらかです。

​Posted by 86