Git 構成ファイルの確認・編集
Git の構成ファイルには次の3種類がある。
システム設定ファイル
ユーザー設定ファイル
リポジトリ設定ファイル
それぞれのファイルについて説明する。
Git 構成ファイルの種類
システム設定ファイル
システム上の全ユーザー・全リポジトリに対する設定が格納される。
ファイルの場所は /etc/gitconfig 。
Windows の場合は C:\Program Files\Git\mingw64\etc\gitconfig 。
システム設定ファイルを設定するには、git config コマンドにオプション --system を指定する。
ユーザー設定ファイル
ユーザー毎の設定が格納される。
ファイルの場所は ~/.gitconfig 。
Windows の場合、C:\Users\〈ユーザー名〉\.gitcocnfig 。環境変数を使って %USERPROFILE%\.gitconfig で参照すると楽。
ユーザー設定ファイルを設定するには、git config コマンドにオプション --global を指定する。
リポジトリ設定ファイル
リポジトリ毎の設定が格納される。
ファイルの場所は 〈リポジトリのフォルダ〉/.git/config 。
リポジトリ設定ファイルを設定するには、git config コマンドにオプション --local を指定する。
Git 構成ファイルの優先順位
ファイルの優先順位は以下のとおり。
数字が小さいほど優先順位が高い。
リポジトリ設定ファイル
ユーザー設定ファイル
システム設定ファイル
たとえばリポジトリ設定ファイルとシステム設定ファイルに同じ設定項目があった場合、リポジトリ設定ファイルの設定が使われる。
Git 構成ファイルの設定を確認するには
git config --list コマンドを使うと、現在の設定値が出力される。
(もし同一のキーが複数出力された場合、最後の値が使われる)
C:\path\to\git-branch> git config --list
http.sslcainfo=C:/Program Files/Git/mingw64/etc/ssl/certs/ca-bundle.crt
http.sslbackend=openssl
core.autocrlf=false
user.name=John Doe
user.email=johndoe@example.com
branch.main.remote=origin
branch.main.merge=refs/heads/main
Git 構成ファイルの場所や設定値のスコープを確認するには
git config --list コマンドにオプションを指定することで、構成ファイルの場所や設定値のスコープを確認できる。
構成ファイルの場所: --show-origin オプションを指定する。
設定値のスコープ: --show-scope オプションを指定する。
例)--show-origin のみ指定する
ファイルの場所が出力される。
C:\path\to\git-branch> git config --show-origin --list
file:C:/Program Files/Git/etc/gitconfig http.sslcainfo=C:/Program Files/Git/mingw64/etc/ssl/certs/ca-bundle.crt
file:C:/Program Files/Git/etc/gitconfig http.sslbackend=openssl
file:C:/Program Files/Git/etc/gitconfig core.autocrlf=false
file:C:/Users/username/.gitconfig user.name=John Doe
file:C:/Users/username/.gitconfig user.email=johndoe@example.com
file:.git/config branch.main.remote=origin
file:.git/config branch.main.merge=refs/heads/main
例)--show-scope のみ指定する
設定値にスコープ(system/global/local)が出力される。
C:\path\to\git-branch> git config --show-scope --list
system http.sslcainfo=C:/Program Files/Git/mingw64/etc/ssl/certs/ca-bundle.crt
system http.sslbackend=openssl
system core.autocrlf=false
global user.name=John Doe
global user.email=johndoe@example.com
local branch.main.remote=origin
local branch.main.merge=refs/heads/main
例)--show-origin と --show-scope の両方を指定する
設定値のスコープとファイルの場所が出力される。
C:\path\to\git-branch> git config --show-origin --show-scope --list
system file:C:/Program Files/Git/etc/gitconfig http.sslcainfo=C:/Program Files/Git/mingw64/etc/ssl/certs/ca-bundle.crt
system file:C:/Program Files/Git/etc/gitconfig http.sslbackend=openssl
system file:C:/Program Files/Git/etc/gitconfig core.autocrlf=false
global file:C:/Users/username/.gitconfig user.name=John Doe
global file:C:/Users/username/.gitconfig user.email=johndoe@example.com
local file:.git/config branch.main.remote=origin
local file:.git/config branch.main.merge=refs/heads/main
Git 構成ファイルに設定を追加するには
git config --add または git config コマンドを使う。
(--add があるかないかで多少挙動が変わる)
たとえば、キー user.name、user.email を追加するコマンドは次のようになる。
git config --add user.name "New Name"
git config user.email "new@example.com"
--add を付けた場合、指定したキーが既に存在すると同じキーで設定が追加されるので留意してほしい。
たとえば user.name が次のように設定されていたとする。
user.name=john doe
そのときに次のコマンドを実行する。
git config --add user.name "New Name"
すると Git 構成ファイルは次のようになる。
user.name=john doe
user.name=New Name
Git 構成ファイルの設定を変更するには
git config コマンドを使う。
該当するキーがあればその設定値が書き換わる。なければ設定が追加される。
たとえば、キー user.name を変更するコマンドは次のようになる。
git config user.name "New Name"
Git 構成ファイルから設定を削除するには
git config --unset コマンドを使う。
たとえば、キー user.name を削除するコマンドは次のようになる。
git config --unset user.name
次のように同じキーが複数存在する場合は「warning: user.name has multiple values」となり削除できない。
user.name=john doe
user.name=New Name
特定の値を削除するにはキーとその値を指定する。
git config --unset user.name "New Name"
すべて削除する場合は --unset ではなく --unset-all を使う。
git config --unset-all user.name