2024/02/13 22:13:39

Git config

[git]

git の挙動を設定する

config ファイルの種類

git の挙動を設定するためのファイルは、bashrc のように、いくつかのファイルに分かれて読み込まれる。

具体的に、以下の順で読み込み解釈され、後に読まれた方が先に読んだ物を上書きしていく。.git/config に書かれた設定は、~/.gitconfig よりも優先される。

  1. システムの設定: /usr/local/etc/gitconfig
  2. ユーザ毎の設定: ~/.gitconfig
  3. リポジトリ設定: .git/config

config ファイルの例

config ファイルの中身は例えば以下のようなものです。

[user]
    name  = foo
    email = foo@example.com
[color]
    ui = true
[alias]
    co = checkout
    st = status
    ci = commit -a
[core]
    pager        = lv -c -Iu8 -Ou8

ini っぽいけれど、独自のフォーマットになっています。
'#' か ';' はコメント行として解釈されます(直接編集すると、コメントが書ける)。

config の編集

config の編集は、ファイルを直接編集してもOKですが、設定するためのコマンドが用意されているので、そちらを利用しましょう。

$ git config [options]

git config コマンドのヘルプは、以下のように読むことができます。

  • git config --help 詳細なヘルプ man git-config と同じ
  • git config -h 簡易ヘルプ

どのコンフィグファイルを編集するかは、オプションで指定します。

  • --system システム全体の設定 git config --system
  • --global ユーザ毎の設定 git config --global
  • -f, --file <FILE> 明示した設定ファイル git config -f .git/config

エディタを使用してファイル編集する場合も、git config(-eオプション) で起動することができます。

$ git config -e --system

git を使い始めて最初に設定するのは、以下の2項目です。

$ git config --global user.name "Foo Bar"
$ git config --global user.email foo@example.com

設定内容は、--list オプションで見ることができます。

$ git config --list

設定項目

git config で設定できる項目は git config --help の Variables に列挙されています。

example

.gitconfig の例

[user]
    name  = foo
    email = foo@example.com
[color]
    ui = true
[alias]
    co = checkout
    st = status
    ci = commit -a
    di = diff
    br = branch
    mg = merge
    wh = whatchanged
    rl = reflog
    stat = log --stat --summary
    logg = log --graph --decorate --pretty=format:\"%ad [%cn] <c:%h t:%t p:%p> %n %Cgreen%d%Creset %s %n\" --name-status
    logs = log --graph --decorate --pretty=format:\"%ad [%cn] <c:%h t:%t p:%p> %n %Cgreen%d%Creset %s %n\" --stat
[core]
    excludesfile = ~/.gitignore
    pager        = lv -c -Iu8 -Ou8
    autocrlf     = input
    preloadindex = true
サイト内検索