Skip to content

Git配置解析

本文介绍如何使用Git的配置。

1. Git配置层级介绍

Git安装完成后,我们可以使用git config来获取和设置配置变量,这些配置控制和影响着Git的各个方面。

这些配置有三个层级:

  • 系统配置:影响该系统上所有用户的所有项目。当使用git config时,加上--system,便可以读取和写入系统配置。一般情况下,该配置文件位置在[path]/etc/gitconfig下,例如,在macOS系统下使用homebrew安装的Git,其系统配置文件路径为/opt/homebrew/etc/gitconfig
  • 用户配置:影响该系统下该用户的所有项目。当使用git config时,加上--global,便可以读取和写入用户配置。一般情况下,该配置文件位置在用户空间下,例如~/.gitconfig~/.config/git/config
  • 项目配置:只影响该项目的配置,位于该项目中的.git/config文件中。当在项目路径下,使用git config时,加上--local,便可以读取和写入项目配置。默认情况下,直接使用git config,就是操作的项目配置。

配置优先级从高到低:项目配置 > 用户配置 > 系统配置,也就是说高优先级中的配置会覆盖低优先级中的同名配置。

2. Git配置文件结构

Git 配置文件本质上是一个 INI 风格(类似 Windows .ini 文件) 的文本文件,由 section(节)key-value(键值对) 组成,每个配置都属于某个节。

  • section:节

    例如,最简单的 section:

    [user]

    表示一个名为 user 的 section。

    下面所有配置都属于这个 section:

    [user]
        name = Tom
        email = tom@example.com

    对应:

    user.name = Tom
    user.email = tom@example.com
  • subsection:子节

    Git 支持 section 带名字:

    [remote "origin"]
    • section = remote
    • subsection = origin

    Git 内部会展开为:

    remote.origin

    因此:

    [remote "origin"]
        url = https://github.com/example/repo.git

    等价于:

    remote.origin.url
  • Key-Value(键值对)

    格式:

    key = value

    例如:

    [core]
        editor = vim

    对应:

    core.editor = vim
  • 多值配置

    同一个 key 可以出现多次:

    [remote "origin"]
        fetch = +refs/heads/main:refs/remotes/origin/main
        fetch = +refs/heads/dev:refs/remotes/origin/dev

    此时:

    git config --get-all remote.origin.fetch

    输出:

    +refs/heads/main:refs/remotes/origin/main
    +refs/heads/dev:refs/remotes/origin/dev

    Git 将其视为一个多值配置。

3. Git配置命令介绍

下面介绍一些常用的Git配置命令。注,以下是新语法。

3.1 get

get:用于获取配置,完整命令如下:

txt
git config get [<file-option>] [<display-option>] [--includes] [--all] [--regexp] [--value=<pattern>] [--fixed-value] [--default=<default>] [--url=<url>] <name>

部分配置解释如下:

  • [<file-option>]:用于指定从哪个配置文件读取。

    常见选项:

    --system
    --global
    --local(默认值)
    --worktree
    --file <file>

    例如,只查询全局配置:

    txt
    git config get --global user.name
  • [<display-option>]:控制输出格式。

    常见:

    --show-origin
    --show-scope

    例如:

    git config get --show-origin user.name

    输出:

    file:/Users/tom/.gitconfig  Tom

    显示配置来源。

  • --all:获取所有配置值。

    例如:

    [remote "origin"]
        fetch = value1
        fetch = value2

    执行:

    git config get --all remote.origin.fetch

    输出:

    value1
    value2

3.2 set

set:用于设置配置值,完整命令如下:

txt
git config set [<file-option>] [--type=<type>] [--all] [--append] [--value=<pattern>] [--fixed-value] <name> <value>

部分命令解释如下:

  • [--type=<type>]:指定数据类型。

    Git 会进行格式转换和校验。支持的常见类型:

    bool
    int
    bool-or-int
    path
    expiry-date
    color

    参考:https://git-scm.com/docs/git-config#Documentation/git-config.txt---typetype

  • --all:处理多值配置。

    假设:

    [remote "origin"]
        fetch = value1
        fetch = value2

    执行:

    git config set remote.origin.fetch newvalue

    Git 会拒绝:

    error: remote.origin.fetch has multiple values

    因为不知道改哪一个。

    使用:

    git config set --all remote.origin.fetch newvalue

    结果:

    [remote "origin"]
        fetch = newvalue

    所有旧值被删除,写入一个新值。

  • [--append]:给多值配置增加一个新值。

    例如,给 remote.origin.fetch 增加一条 fetch 规则:

    当前配置:

    [remote "origin"]
        fetch = +refs/heads/main:refs/remotes/origin/main

    执行:

    git config set --append \
        remote.origin.fetch \
        '+refs/heads/dev:refs/remotes/origin/dev'

    结果:

    [remote "origin"]
        fetch = +refs/heads/main:refs/remotes/origin/main
        fetch = +refs/heads/dev:refs/remotes/origin/dev
  • [--value=<pattern>]:只修改匹配某个值的配置项。

    例如:

    [test]
        key = aaa
        key = bbb
        key = ccc

    执行:

    git config set --value=bbb test.key  xyz

    结果:

    txt
    [test]
        key = aaa
        key = xyz
        key = ccc
  • --fixed-value:控制 --value= 的匹配方式。

    默认:

    --value=<pattern>

    是正则表达式。例如:

    --value='a.*'

    匹配:

    aaa
    abc
    a123

    如果加了--fixed-value,表示按字面字符串匹配。那么--value='a.*' --fixed-value,只会匹配a.*

3.3 list

list:显示所有配置,完整命令如下:

txt
git config list [<file-option>] [<display-option>] [--includes]

3.4 unset

unset:用于删除配置

txt
git config unset [<file-option>] [--all] [--value=<pattern>] [--fixed-value] <name>
  • [--all]:默认情况下,unset不会删除多值配置,加上--all就会删除多值配置;
  • [--value=<pattern>]:删除值满足正则表达式的配置;配合--fixed-value表示--value为字面字符串匹配;

3.5 rename-section

rename-section:对节进行重命名,完整命令如下:

txt
git config rename-section [<file-option>] <old-name> <new-name>

3.6 remove-section

remove-section:删除指定节,完整命令如下:

txt
git config remove-section [<file-option>] <name>

3.7 edit

edit:打开编辑器,修改指定的配置文件

txt
git config edit [<file-option>]

4. 常用配置项

本小节介绍一些常用配置项。

  • user.name:提交者名称;

  • user.email:提交者邮箱;

  • core.editor:Git 需要编辑文本时使用的编辑器;例如使用vim

    txt
    git config set --global core.editor vim
  • init.defaultBranch:新仓库默认分支名,例如:

    txt
    git config set --global init.defaultBranch main

    之后使用git init创建新仓库时,默认分支名就是main,而不是master了。

参考资料

[1] https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup

[2] https://git-scm.com/docs/git-config