はじめに#
Gitを使い始めたとき、最初にgit configでユーザー名とメールアドレスを設定した経験がある方は多いでしょう。しかし、Git Configにはそれ以外にも数百もの設定項目があり、適切にカスタマイズすることで開発効率を大幅に向上させることができます。
本記事では、Git Configの仕組みから.gitconfigファイルの役割、設定の階層構造、そして実務で役立つおすすめ設定例まで徹底的に解説します。この記事を読み終えると、以下のことができるようになります。
- Git Configの仕組みと設定ファイルの階層を理解できる
- システム、グローバル、ローカル設定の違いを説明できる
- .gitconfigファイルを直接編集してカスタマイズできる
includeIfを使った条件付き設定を活用できる
- 開発効率を向上させるおすすめ設定を自分の環境に導入できる
実行環境と前提条件#
本記事の内容は、以下の環境で動作確認を行っています。
| 項目 |
要件 |
| Git |
2.40以上 |
| OS |
Windows 10/11、macOS 12以上、Ubuntu 22.04以上 |
| ターミナル |
コマンドプロンプト、PowerShell、Terminal.app、bash等 |
| エディタ |
VS Code推奨 |
前提条件として、以下の知識があることを想定しています。
- コマンドライン操作の基礎知識(
cd、ls/dir、mkdir等)
- テキストエディタの基本操作
Git Configとは#
Git Config(git config)は、Gitの動作を制御する設定を管理するコマンドおよび仕組みです。ユーザー情報、エディタの指定、差分表示のカスタマイズなど、Gitのあらゆる動作をカスタマイズできます。
Git Configの役割#
Git Configには主に以下の役割があります。
| 役割 |
説明 |
| ユーザー識別 |
コミット時の作成者情報を設定 |
| 動作制御 |
push/pull/merge等のデフォルト動作を指定 |
| ツール連携 |
エディタや差分ツールとの連携を設定 |
| 表示カスタマイズ |
カラー表示やログフォーマットを調整 |
Git Configの基本構文#
Git Configの設定値は、セクションとキーの組み合わせで表現されます。
1
2
|
# 構文: git config [スコープ] セクション.キー 値
git config --global user.name "Your Name"
|
上記の例では、userがセクション、nameがキー、"Your Name"が値となります。
Git Config設定の階層構造#
Git Configには3つの設定階層(スコープ)があり、それぞれ異なるファイルに保存されます。より狭いスコープの設定が優先されるため、プロジェクトごとに異なる設定を適用することが可能です。
設定階層の優先順位#
設定は以下の順序で読み込まれ、後から読み込まれた設定が優先されます。
| スコープ |
オプション |
適用範囲 |
優先度 |
| システム |
--system |
マシン全体の全ユーザー |
低 |
| グローバル |
--global |
現在のユーザーの全リポジトリ |
中 |
| ローカル |
--local |
現在のリポジトリのみ |
高 |
各スコープの設定ファイルの場所#
各スコープの設定ファイルは、OSによって保存場所が異なります。
Windowsの場合
| スコープ |
ファイルパス |
| システム |
C:\Program Files\Git\etc\gitconfig |
| グローバル |
C:\Users\<ユーザー名>\.gitconfig |
| ローカル |
<リポジトリ>\.git\config |
macOS/Linuxの場合
| スコープ |
ファイルパス |
| システム |
/etc/gitconfig |
| グローバル |
~/.gitconfig または ~/.config/git/config |
| ローカル |
<リポジトリ>/.git/config |
現在の設定を確認する方法#
現在適用されている設定を確認するには、以下のコマンドを使用します。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# すべての設定を一覧表示
git config --list
# 設定値の出所も含めて表示
git config --list --show-origin
# スコープも表示
git config --list --show-scope
# 特定の設定値を確認
git config user.name
# 特定のスコープの設定のみ表示
git config --global --list
|
実行例を確認してみましょう。
1
2
3
4
5
|
$ git config --list --show-scope --show-origin
system file:C:/Program Files/Git/etc/gitconfig core.autocrlf=true
global file:C:/Users/user/.gitconfig user.name=Your Name
global file:C:/Users/user/.gitconfig user.email=your@email.com
local file:.git/config core.repositoryformatversion=0
|
この出力から、各設定がどのファイルから読み込まれているかを確認できます。
Git Configの設定方法#
Git Configの設定方法には、コマンドを使用する方法と設定ファイルを直接編集する方法の2つがあります。
git configコマンドによる設定#
最も基本的な方法は、git configコマンドを使用することです。
1
2
3
4
5
6
7
8
9
10
11
12
|
# グローバル設定に追加
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
# ローカル設定に追加(リポジトリ内で実行)
git config --local user.email "work@company.com"
# 設定を削除
git config --global --unset user.name
# セクション全体を削除
git config --global --remove-section alias
|
設定ファイルの直接編集#
エディタを使用して設定ファイルを直接編集することも可能です。
1
2
3
4
5
|
# グローバル設定ファイルをエディタで開く
git config --global --edit
# ローカル設定ファイルをエディタで開く
git config --local --edit
|
設定ファイルはINI形式で記述されています。
1
2
3
4
5
6
7
8
9
10
11
|
[user]
name = Your Name
email = your@email.com
[core]
editor = code --wait
autocrlf = true
[alias]
st = status
co = checkout
|
おすすめのGit Config設定#
ここからは、実務で役立つおすすめのGit Config設定を紹介します。
ユーザー情報の設定#
コミット時に記録される作成者情報を設定します。これはGitを使い始める際に必須の設定です。
1
2
3
4
5
|
# ユーザー名の設定
git config --global user.name "Your Name"
# メールアドレスの設定
git config --global user.email "your@email.com"
|
設定ファイルでは以下のように記述します。
1
2
3
|
[user]
name = Your Name
email = your@email.com
|
エディタの設定#
コミットメッセージの編集やリベース時に使用するエディタを指定します。
1
2
3
4
5
6
7
8
|
# VS Codeを使用する場合
git config --global core.editor "code --wait"
# Vimを使用する場合
git config --global core.editor "vim"
# Nano を使用する場合
git config --global core.editor "nano"
|
--waitオプションは、VS Codeがファイルを閉じるまでGitが待機するために必要です。
差分ツールとマージツールの設定#
外部の差分ツールやマージツールを使用する場合は、以下のように設定します。
1
2
3
4
5
6
7
|
# 差分ツールの設定(VS Code)
git config --global diff.tool vscode
git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'
# マージツールの設定(VS Code)
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd 'code --wait $MERGED'
|
設定ファイルでは以下のように記述します。
1
2
3
4
5
6
7
8
9
10
11
12
|
[diff]
tool = vscode
[difftool "vscode"]
cmd = code --wait --diff $LOCAL $REMOTE
[merge]
tool = vscode
[mergetool "vscode"]
cmd = code --wait $MERGED
keepBackup = false
|
カラー表示の設定#
ターミナル出力のカラー表示を有効にすると、視認性が向上します。
1
2
3
4
5
6
7
|
# カラー表示を自動で有効化(推奨)
git config --global color.ui auto
# 特定のコマンドでカラーを有効化
git config --global color.status auto
git config --global color.diff auto
git config --global color.branch auto
|
設定ファイルでは以下のように記述します。
1
2
3
4
5
|
[color]
ui = auto
status = auto
diff = auto
branch = auto
|
push/pullの挙動設定#
push/pullコマンドのデフォルト動作をカスタマイズします。
1
2
3
4
5
6
7
8
9
10
11
|
# pushのデフォルト動作を現在のブランチに設定
git config --global push.default current
# push時に上流ブランチを自動設定
git config --global push.autoSetupRemote true
# pull時にリベースを使用(推奨)
git config --global pull.rebase true
# fast-forwardのみ許可
git config --global pull.ff only
|
設定ファイルでは以下のように記述します。
1
2
3
4
5
6
7
|
[push]
default = current
autoSetupRemote = true
[pull]
rebase = true
ff = only
|
push.defaultの設定値には以下のオプションがあります。
| 値 |
説明 |
current |
現在のブランチと同名のリモートブランチにpush |
simple |
上流ブランチが同名の場合のみpush(デフォルト) |
upstream |
上流ブランチにpush |
nothing |
明示的な指定がないとpushしない |
デフォルトブランチ名の設定#
新規リポジトリ作成時のデフォルトブランチ名を設定します。
1
|
git config --global init.defaultBranch main
|
設定ファイルでは以下のように記述します。
1
2
|
[init]
defaultBranch = main
|
グローバルignoreファイルの設定#
OSやエディタが生成する一時ファイルなど、すべてのリポジトリで無視したいファイルを設定します。
1
2
|
# グローバルignoreファイルのパスを設定
git config --global core.excludesFile ~/.gitignore_global
|
設定ファイルでは以下のように記述します。
1
2
|
[core]
excludesFile = ~/.gitignore_global
|
~/.gitignore_globalファイルの例を以下に示します。
# OS関連
.DS_Store
Thumbs.db
Desktop.ini
# エディタ関連
*.swp
*.swo
*~
.idea/
.vscode/
# 依存関係
node_modules/
vendor/
改行コードの設定#
Windows/macOS/Linux間での改行コードの差異を自動的に処理します。
1
2
3
4
5
|
# Windowsの場合
git config --global core.autocrlf true
# macOS/Linuxの場合
git config --global core.autocrlf input
|
| 設定値 |
動作 |
true |
チェックアウト時にLFをCRLFに変換、コミット時にCRLFをLFに変換 |
input |
コミット時のみCRLFをLFに変換 |
false |
変換しない |
SSL証明書の設定#
社内プロキシ環境など、カスタムCA証明書が必要な場合に設定します。
1
2
3
4
5
|
# カスタムCA証明書を指定
git config --global http.sslCAInfo /path/to/ca-bundle.crt
# 特定のホストのみSSL検証を無効化(非推奨)
git config --global http.https://internal.example.com/.sslVerify false
|
認証情報のキャッシュ設定#
HTTPS接続時のパスワード入力を省略するため、認証情報をキャッシュできます。
1
2
3
4
5
6
7
8
9
10
11
|
# Windowsの場合(Credential Manager使用)
git config --global credential.helper manager
# macOSの場合(Keychain使用)
git config --global credential.helper osxkeychain
# Linuxの場合(一時キャッシュ、15分)
git config --global credential.helper cache
# Linuxの場合(キャッシュ時間を1時間に設定)
git config --global credential.helper 'cache --timeout=3600'
|
includeIfによる条件付き設定#
includeIfディレクティブを使用すると、特定の条件に基づいて異なる設定ファイルを読み込むことができます。これは仕事用とプライベート用でGitの設定を分けたい場合に非常に便利です。
includeIfの基本構文#
includeIfは以下の形式で記述します。
1
2
|
[includeIf "条件:パターン"]
path = 読み込む設定ファイルのパス
|
ディレクトリによる条件分岐#
最も一般的な使用方法は、リポジトリのディレクトリパスに基づいて設定を切り替えることです。
~/.gitconfigファイルに以下のように記述します。
1
2
3
4
5
6
7
8
9
10
11
|
[user]
name = Private Name
email = private@example.com
# 仕事用ディレクトリの場合
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-work
# OSS活動用ディレクトリの場合
[includeIf "gitdir:~/oss/"]
path = ~/.gitconfig-oss
|
~/.gitconfig-workファイルを作成します。
1
2
3
4
5
6
|
[user]
name = Work Name
email = work@company.com
[core]
sshCommand = ssh -i ~/.ssh/id_rsa_work
|
~/.gitconfig-ossファイルを作成します。
1
2
3
4
5
6
|
[user]
name = OSS Contributor Name
email = oss@example.com
[core]
sshCommand = ssh -i ~/.ssh/id_rsa_oss
|
この設定により、~/work/配下のリポジトリでは仕事用の設定が、~/oss/配下のリポジトリではOSS用の設定が自動的に適用されます。
includeIfで使用できる条件#
includeIfでは以下の条件キーワードを使用できます。
| 条件 |
説明 |
gitdir: |
リポジトリのパスにマッチする場合に適用 |
gitdir/i: |
大文字小文字を区別せずにパスマッチ(Windows向け) |
onbranch: |
特定のブランチがチェックアウトされている場合に適用 |
hasconfig:remote.*.url: |
特定のリモートURLを持つ場合に適用 |
ブランチによる条件分岐#
特定のブランチでのみ異なる設定を適用することもできます。
1
2
3
4
5
6
7
|
# mainブランチでの作業時
[includeIf "onbranch:main"]
path = ~/.gitconfig-main
# feature/ブランチでの作業時
[includeIf "onbranch:feature/**"]
path = ~/.gitconfig-feature
|
リモートURLによる条件分岐#
リモートURLに基づいて設定を切り替えることも可能です。
1
2
3
4
5
6
7
|
# GitHubリポジトリの場合
[includeIf "hasconfig:remote.*.url:https://github.com/**"]
path = ~/.gitconfig-github
# GitLabリポジトリの場合
[includeIf "hasconfig:remote.*.url:https://gitlab.com/**"]
path = ~/.gitconfig-gitlab
|
includeIf使用時の注意点#
includeIfを使用する際の注意点を以下にまとめます。
- パスの末尾にスラッシュ(
/)を付けると、そのディレクトリ配下すべてにマッチします
- Windowsでは
gitdir/i:を使用して大文字小文字を区別しないマッチングを推奨します
- 条件は上から順に評価され、複数の条件にマッチする場合はすべての設定が読み込まれます
- 後から読み込まれた設定が優先されます
おすすめの.gitconfig設定例#
ここまで解説した設定をまとめた、おすすめの.gitconfig設定例を紹介します。
基本設定テンプレート#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
[user]
name = Your Name
email = your@email.com
[init]
defaultBranch = main
[core]
editor = code --wait
autocrlf = input
excludesFile = ~/.gitignore_global
quotepath = false
pager = less -F -X
[color]
ui = auto
[push]
default = current
autoSetupRemote = true
[pull]
rebase = true
ff = only
[fetch]
prune = true
[diff]
tool = vscode
colorMoved = default
[difftool "vscode"]
cmd = code --wait --diff $LOCAL $REMOTE
[merge]
tool = vscode
conflictstyle = diff3
[mergetool "vscode"]
cmd = code --wait $MERGED
keepBackup = false
[alias]
st = status
co = checkout
br = branch
ci = commit
lg = log --oneline --graph --decorate
unstage = reset HEAD --
last = log -1 HEAD
[credential]
helper = manager
|
条件付き設定を含むテンプレート#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
[user]
name = Private Name
email = private@example.com
[init]
defaultBranch = main
[core]
editor = code --wait
autocrlf = input
excludesFile = ~/.gitignore_global
[color]
ui = auto
[push]
default = current
autoSetupRemote = true
[pull]
rebase = true
[fetch]
prune = true
[alias]
st = status
co = checkout
br = branch
ci = commit
lg = log --oneline --graph --decorate
# 仕事用ディレクトリ
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-work
# OSS用ディレクトリ
[includeIf "gitdir:~/oss/"]
path = ~/.gitconfig-oss
|
Git Config設定の確認とトラブルシューティング#
設定が正しく適用されているかを確認する方法と、よくある問題の解決方法を紹介します。
設定の適用状況を確認する#
1
2
3
4
5
6
7
8
|
# 特定の設定値がどこで設定されているか確認
git config --show-origin user.email
# 実際に適用される値を確認
git config user.email
# すべての設定と出所を確認
git config --list --show-origin --show-scope
|
よくあるトラブルと解決方法#
設定が反映されない場合
より狭いスコープで同じ設定が上書きされている可能性があります。--show-originオプションで確認してください。
1
2
|
$ git config --show-origin user.email
file:/home/user/.gitconfig private@example.com
|
includeIfが動作しない場合
- パスの末尾にスラッシュがあるか確認してください
- Windowsの場合は
gitdir/i:を使用してください
- 指定したパスにファイルが存在するか確認してください
1
2
|
# 設定ファイルのパスを確認
git config --list --show-origin | grep includeIf
|
文字化けが発生する場合
日本語ファイル名が正しく表示されない場合は、以下の設定を追加してください。
1
|
git config --global core.quotepath false
|
まとめ#
本記事では、Git Configの仕組みから.gitconfigのカスタマイズ方法、おすすめの設定例までを解説しました。
Git Configを理解し適切に設定することで、以下のメリットが得られます。
- 開発効率の向上: エディタやツールとのシームレスな連携
- ミスの防止: push/pullのデフォルト動作を安全な設定に
- 柔軟な運用: includeIfによる仕事とプライベートの設定分離
- チーム開発の円滑化: 一貫した設定による環境差異の削減
まずは本記事で紹介した基本設定テンプレートをベースに、自分の開発環境に合わせてカスタマイズしてみてください。
参考リンク#