今回はgit導入時の初期設定や設定ファイル(/etc/gitconfig や ~/.gitconfig )について記載しております。
- はじめに
- gitの設定箇所と役割について
- 各レベルの比較
- /etc/gitconfig の具体的な設定について
- 上記/etc/gitconfig の設定コマンド
- ~/.gitconfig の具体的な設定について
- 上記~/.gitconfig の設定コマンド
はじめに
gitのインストール方法については記載しておりません。
本記事では、git初期設定で登場する各種ファイルについての説明です。
gitの設定箇所と役割について
Gitの設定ファイルは主に以下の3つのレベルで管理されます。
システムレベル(System-level):
/etc/gitconfigファイルに設定が保存されます。- このレベルの設定は、システム上のすべてのユーザーとすべてのリポジトリに適用されます。
- 設定を変更するには通常、管理者権限が必要です。
ユーザーレベル(User-level):
- ユーザーのホームディレクトリにある
.gitconfigまたは.config/git/configファイルに設定が保存されます。 - このレベルの設定は、そのユーザーのすべてのリポジトリに適用されます。
- ユーザーのホームディレクトリにある
リポジトリレベル(Repository-level):
- 各リポジトリの
.git/configファイルに設定が保存されます。 - このレベルの設定は、その特定のリポジトリにのみ適用されます。
- 各リポジトリの
これらの設定ファイルは、設定の優先順位に応じて上書きされます。
リポジトリレベルの設定が最も優先され、次にユーザーレベル、最後にシステムレベルの設定が適用されます。
各レベルでの設定値を確認するには、以下のコマンドを使用します。
- システムレベルの設定を表示:
git config --system --list
- ユーザーレベルの設定を表示:
git config --global --list
- リポジトリレベルの設定を表示:
git config --local --list
- 全設定を表示:
git config --list
各レベルの比較
各レベルを表で比較したものです。
| レベル | ファイルの場所 | 適用範囲 | 備考 | 優先度/強弱 |
|---|---|---|---|---|
| システムレベル | /etc/gitconfig |
システム上の全てのユーザーとリポジトリ | 管理者権限が必要な場合がある | 低/弱 |
| ユーザーレベル | ~/.gitconfig または ~/.config/git/config |
特定のユーザーの全てのリポジトリ | 個々のユーザー設定 | 中/普通 |
| リポジトリレベル | .git/config |
特定のリポジトリ | リポジトリ固有の設定 | 高/強 |
/etc/gitconfig の具体的な設定について
/etc/gitconfig の設定例です。
下記設定があった場合、その内容は以下のとおりです。
[http]
proxy = http://proxy.test.com:8080
[https]
proxy = http://proxy.test.com:8080
[http "https://proxy-example.com"]
proxy =
[http] と [https] セクション:
-
proxy = http://proxy.test.com:8080: これはGitがHTTPまたはHTTPS経由でリモートリポジトリにアクセスする際に、http://proxy.test.com:8080というプロキシサーバーを使用するように設定しています。これにより、このプロキシ経由でのみ外部リポジトリへのアクセスが可能になります。
[http "https://proxy-example.com"] セクション:
-
proxy =: この特定の設定は、https://proxy-example.comドメインに対してはプロキシを使用しないように指定しています。つまり、このドメインに対するアクセスは直接行われ、上記で指定されたプロキシサーバーはバイパスされます。
上記/etc/gitconfig の設定コマンド
/etc/gitconfig ファイルに記載されている設定内容は前述しておりますが、では実際にどうやって設定するのかについてです。
- HTTPとHTTPSのプロキシ設定:
git config --system http.proxy http://proxy.test.com:8080 git config --system https.proxy http://proxy.test.com:8080
- 特定のドメイン(
https://proxy-example.com)に対するプロキシの除外設定:
git config --system --unset http.https://proxy-example.com.proxy
- 設定値を確認:
git config --system --list
~/.gitconfig の具体的な設定について
~/.gitconfig の設定例です。
下記設定があった場合、その内容は以下のとおりです。
[user]
name = Your Name
email = your.email@example.com
[commit]
gpgSign = true
template = ~/.git_commit_template.txt
[core]
editor = vim
[init]
defaultBranch = main
[merge]
ff = true
tool = meld
[rebase]
autosquash = true
[fetch]
prune = true
[push]
default = simple
[http]
proxy = http://proxy.example.com:8080
sslVerify = false
[http "http://example.com"]
proxy =
[https]
proxy = https://proxy.example.com:8080
[https "https://example.com"]
proxy =
[color]
ui = true
[alias]
st = status
ci = commit
br = branch
co = checkout
lg = log --graph --decorate --pretty=oneline --abbrev-commit
[user]セクション:
user.name: コミット時に使用されるユーザー名。user.email: コミット時に使用されるメールアドレス。
[commit]セクション:
commit.gpgSign: コミットにGPG署名を自動的に追加するかどうか。commit.template: コミットメッセージのテンプレートファイルのパス。
[core]セクション:
core.editor: Gitコマンドで使用するデフォルトのテキストエディタ。
[init]セクション:
init.defaultBranch:git initで新規リポジトリを作成したときのデフォルトのブランチ名。
[merge]セクション:
merge.ff: ファストフォワードマージを許可するかどうか。merge.tool: マージの際に使用するデフォルトのツール。
[rebase]セクション:
rebase.autosquash: リベース時に自動的にスカッシュするかどうか。
[fetch]セクション:
fetch.prune:git fetch実行時にローカルの追跡されなくなったブランチを削除するかどうか。
[push]セクション:
push.default:git push実行時のデフォルトの動作(例:matching,simple)。
[http]および[https]セクション:
http.proxy: HTTPプロキシサーバーのURL。https.proxy: HTTPSプロキシサーバーのURL。- 特定のURLに対しては
proxy=と指定してプロキシを経由しない(設定除外)。
[color]セクション:
color.ui: Gitコマンドの出力でカラーを使用するかどうか。
[alias]セクション:
alias.<name>: よく使用するGitコマンドに短縮形を設定。
上記~/.gitconfig の設定コマンド
~/.gitconfig ファイルに記載されている設定内容は前述しておりますが、では実際にどうやって設定するのかについてです。
# ユーザー情報 git config --global user.name "Your Name" git config --global user.email "your.email@example.com" # コミット設定 git config --global commit.gpgSign true git config --global commit.template ~/.git_commit_template.txt # コア設定 git config --global core.editor vim # 初期ブランチ名 git config --global init.defaultBranch main # マージ設定 git config --global merge.ff true git config --global merge.tool meld # リベース設定 git config --global rebase.autosquash true # フェッチ設定 git config --global fetch.prune true # プッシュ設定 git config --global push.default simple # プロキシ設定 git config --global http.proxy http://proxy.example.com:8080 git config --global http.sslVerify false git config --global http."http://example.com".proxy "" git config --global https.proxy https://proxy.example.com:8080 git config --global https."https://example.com".proxy "" # カラー設定 git config --global color.ui true # エイリアス設定 git config --global alias.st status git config --global alias.ci commit git config --global alias.br branch git config --global alias.co checkout git config --global alias.lg "log --graph --decorate --pretty=oneline --abbrev-commit"
以上です。