Git命令列表–git-config

git config 查看、编辑Git的配置文件

配置文件的范围和语法

$ git config
usage: it config []

Config file location
    --global              use global config file/全局配置文件,对当前用户的所有项目适用,在用户目录中,如:C:\Users\xuyuansheng\.gitconfig
    --system              use system config file/系统配置文件, 对系统中所有用户都普遍适用的配置,位置一般在软件安装的目录,如:D:\Java Program Files\Git\etc\gitconfig
    --local               use repository config file/仓库本地配置文件
    --worktree            use per-worktree config file/工作树的配置文件,在仓库的.git/worktrees/工作树名称/config.worktree中。详情见git help worktree 中的CONFIGURATION FILE部分。
    -f, --file      use given config file/指定(特定)的配置文件
    --blob       read config from given blob object/git仓库中管理的配置文件,用过blob-id使用

Action
    --get                 get value: name [value-pattern]/获取配置
    --get-all             get all values: key [value-pattern]/获取所有配置、包括多个值的配置
    --get-regexp          get values for regexp: name-regex [value-pattern]/
    --get-urlmatch        get value specific for the URL: section[.var] URL
    --replace-all         replace all matching variables: name value [value-pattern]/替换所有匹配到的值
    --add                 add a new variable: name value/添加一个新的值
    --unset               remove a variable: name [value-pattern]/删除值
    --unset-all           remove all matches: name [value-pattern]/删除所有的值
    --rename-section      rename section: old-name new-name/重新命名键的部分(section)名称
    --remove-section      remove a section: name/移除一个键的部分(section)名称
    -l, --list            list all/显示所有
    --fixed-value         use string equality when comparing values to 'value-pattern'
    -e, --edit            open an editor/打开配置编辑器
    --get-color           find the color configured: slot [default]
    --get-colorbool       find the color setting: slot [stdout-is-tty]

Type
    -t, --type <>         value is given this type
    --bool                value is "true" or "false"
    --int                 value is decimal number
    --bool-or-int         value is --bool or --int
    --bool-or-str         value is --bool or string
    --path                value is a path (file or directory name)
    --expiry-date         value is an expiry date

Other
    -z, --null            terminate values with NUL byte/对于输出值和/或键的所有选项,始终以空字符(而不是换行符)结束值。 使用换行符作为键和值之间的分隔符。 这允许安全地解析输出而不会混淆,例如 按包含换行符的值
    --name-only           show variable names only/只显示名称
    --includes            respect include directives on lookup
    --show-origin         show origin of config (file, standard input, blob, command line)/显示配置的来源
    --show-scope          show scope of config (worktree, local, global, system, command)/显示配置范围
    --default      with --get, use default value when missing entry

简单命令示例

以下的操作示例均在worktree范围,其他范围的配置类比即可,可选参数如下: –global、 –system、–local 、–worktree、-f, –file、–blob

user@name MINGW64 /d/VSCode/wt2 (wt2)
$ git config --worktree c.a va

user@NAME MINGW64 /d/VSCode/wt2 (wt2)
$ git config --worktree --list
c.a=va
user@NAME MINGW64 /d/VSCode/wt2 (wt2)
$ git config --worktree --add  c.a va
$ git config --worktree --add  c.a val1
$ git config --worktree --list
c.a=va
c.a=va
user@NAME MINGW64 /d/VSCode/wt2 (wt2)
$ git config --worktree --get c.a
val1
$ git config --worktree --get-all c.a
va
va
val1
user@NAME MINGW64 /d/VSCode/wt2 (wt2)
git config --worktree c.b  valb
git config --worktree c.c valc
user@NAME MINGW64 /d/VSCode/wt2 (wt2)
$ git config --worktree  --list
c.a=va
c.a=va
c.a=val1
c.b=valb
c.c=valc
user@NAME MINGW64 /d/VSCode/wt2 (wt2)
##  --unset 不能删除多值配置
$ git config --worktree  --unset c.a
warning: c.a has multiple values

$ git config --worktree  --unset c.b
## c.b 被删除
$ git config --worktree  -l
c.a=va
c.a=va
c.a=val1
c.c=valc
## --unset-all 可以删除多值和单值
user@NAME MINGW64 /d/VSCode/wt2 (wt2)
$ git config --worktree  --unset-all c.c

user@NAME MINGW64 /d/VSCode/wt2 (wt2)
$ git config --worktree  -l
c.a=va
c.a=va
c.a=val1

user@NAME MINGW64 /d/VSCode/wt2 (wt2)
$ git config --worktree  --unset-all c.a

user@NAME MINGW64 /d/VSCode/wt2 (wt2)
$ git config --worktree  -l
user@NAME MINGW64 /d/VSCode/wt2 (wt2)
$ git config --worktree  c.a  val-1

user@NAME MINGW64 /d/VSCode/wt2 (wt2)
$ git config --worktree  -l
c.a=val-1

user@NAME MINGW64 /d/VSCode/wt2 (wt2)
$ git config --worktree  c.a  val-2

user@NAME MINGW64 /d/VSCode/wt2 (wt2)
$ git config --worktree  -l
c.a=val-2
user@NAME MINGW64 /d/VSCode/wt2 (wt2)
$ git config --worktree --rename-section ccc bb
fatal: no such section: ccc

user@NAME MINGW64 /d/VSCode/wt2 (wt2)
$ git config --worktree --rename-section c c1

user@NAME MINGW64 /d/VSCode/wt2 (wt2)
$ git config --worktree -l
c1.a=val-2
## 先添加几个配置组 c2,c3
user@NAME MINGW64 /d/VSCode/wt2 (wt2)
$ git config --worktree  c2.cc  cc1

user@NAME MINGW64 /d/VSCode/wt2 (wt2)
$ git config --worktree  c2.bb  bb1

user@NAME MINGW64 /d/VSCode/wt2 (wt2)
$ git config --worktree  c3.bb  bb1

user@NAME MINGW64 /d/VSCode/wt2 (wt2)
$ git config --worktree  -l
c1.a=val-2
c2.cc=cc1
c2.bb=bb1
c3.bb=bb1
## 删除键的部分(section)
user@NAME MINGW64 /d/VSCode/wt2 (wt2)
$ git config --worktree  --remove-section c1

user@NAME MINGW64 /d/VSCode/wt2 (wt2)
$ git config --worktree  -l
c2.cc=cc1
c2.bb=bb1
c3.bb=bb1

user@NAME MINGW64 /d/VSCode/wt2 (wt2)
$ git config --worktree  --remove-section c2

user@NAME MINGW64 /d/VSCode/wt2 (wt2)
$ git config --worktree  -l
c3.bb=bb1

配置文件内容
[http]
        sslVerify
[http "https://weak.example.com"]
        sslVerify = false
        cookieFile = /tmp/cookie.txt
## 命令
$ git config --type=bool --get-urlmatch http.sslverify https://weak.example.com
false

$ git config --worktree  --get-urlmatch http https://weak.example.com
http.cookiefile /tmp/cookie.txt
http.sslverify false

官方语法参考

git config [] [--type=] [--fixed-value] [--show-origin] [--show-scope] [-z|--null] name [value [value-pattern]]
git config [] [--type=] --add name value
git config [] [--type=] [--fixed-value] --replace-all name value [value-pattern]
git config [] [--type=] [--show-origin] [--show-scope] [-z|--null] [--fixed-value] --get name [value-pattern]
git config [] [--type=] [--show-origin] [--show-scope] [-z|--null] [--fixed-value] --get-all name [value-pattern]
git config [] [--type=] [--show-origin] [--show-scope] [-z|--null] [--fixed-value] [--name-only] --get-regexp name_regex [value-pattern]
git config [] [--type=] [-z|--null] --get-urlmatch name URL
git config [] [--fixed-value] --unset name [value-pattern]
git config [] [--fixed-value] --unset-all name [value-pattern]
git config [] --rename-section old_name new_name
git config [] --remove-section name
git config [] [--show-origin] [--show-scope] [-z|--null] [--name-only] -l | --list
git config [] --get-color name [default]
git config [] --get-colorbool name [stdout-is-tty]
git config [] -e | --edit
  • [] 配置文件范围选项
  • [–type=] 值的类型,–type= 选项指示 git config 确保传入和传出值在给定 下是可规范化的。 如果没有给出 –type= ,则不会执行规范化。 调用者可以使用 –no-type 取消设置现有的 –type 说明符
  • [–fixed-value]
  • [–show-origin] 显示配置的来源,如(file, standard input, blob, command line)
  • [–show-scope] 显示配置范围,如(worktree, local, global, system, command)
  • [–name-only] 只显示名称
  • [-z|–null] 对于输出值和/或键的所有选项,始终以空字符(而不是换行符)结束值。 使用换行符作为键和值之间的分隔符。 这允许安全地解析输出而不会混淆,例如 按包含换行符的值
  • [value-pattern] 这是一个扩展的正则表达式,除非给出了 –fixed-value 选项

您可以使用此命令查询/设置/替换/取消设置选项。 名称实际上是由点分隔的部分和键,值将被转义。
使用 –add 选项可以将多行添加到一个选项中。 如果要更新或取消设置可能出现在多行上的选项,则需要给出值模式(这是一个扩展的正则表达式,除非给出了 –fixed-value 选项)。 只有与模式匹配的现有值才会更新或取消设置。 如果要处理与模式不匹配的行,只需在前面添加一个感叹号(另请参见示例),但请注意,这仅在不使用 –fixed-value 选项时有效。

user@NAME MINGW64 /d/VSCode/wt2 (wt2)
##  --worktree
$ git config --worktree --get c3.bb
cc
##  --local
$ git config --local  --get core.ignorecase
true
##  --blob
$ git config --blob=890ad307962dd370f70c3f8d3af180c9c59a909f  --get blob.bare
false
##  blob 不能修改
$ git config --blob=890ad307962dd370f70c3f8d3af180c9c59a909f blob.ignorecase false
fatal: writing config blobs is not supported
##  -f /--file
$ git config -f D:\\VSCode\\testGit\\blob.config  --get blob.bare
false

## 无论是获取或者修改(添加)配置 都会校验值的类型
xuyuansheng@XUYUANSHENG MINGW64 /d/VSCode/testGit (master)
$ git config  -t bool  init.defaultbranch  main
fatal: bad boolean config value 'main' for 'init.defaultbranch'

xuyuansheng@XUYUANSHENG MINGW64 /d/VSCode/testGit (master)
$ git config  -t bool  --get init.defaultbranch
fatal: bad boolean config value 'master' for 'init.defaultbranch'
## 配置文件内容
$ git config --worktree -l
core.filemode=false
diff.external=/usr/local/bin/diff-wrapper
diff.renames=true
core.gitproxy=proxy-command for kernel.org
core.gitproxy=default-proxy
http.sslverify
http.https://weak.example.com.sslverify=false
http.https://weak.example.com.cookiefile=/tmp/cookie.txt
##  获取 core.gitproxy 的值
$ git config --get core.gitproxy
default-proxy  ## 只获取到了最后一个值
## 通过正则表达式获取到了 for kernel.org 结尾的值
$ git config --get core.gitproxy "for kernel.org$"
proxy-command for kernel.org
## 获取所有的值
$  git config --get-all core.gitproxy
proxy-command for kernel.org
default-proxy
## 修改不包含for的 core.gitproxy 配置
$ git config --worktree core.gitproxy ssh '! for '
$  git config --get-all core.gitproxy
proxy-command for kernel.org
ssh

CONFIGURATION FILE

Git 配置文件包含许多影响 Git 命令行为的变量。 每个存储库中的文件 .git/config 和可选的 config.worktree用于存储该存储库的配置,$HOME/.gitconfig 用于存储 每个用户的配置作为 .git/config 文件的后备值。 /etc/gitconfig 可用于存储系统范围的默认配置。

配置变量被 Git 管道和瓷器使用。 变量被划分为段,其中变量本身的完全限定变量名是最后一个以点分隔的段,段名是最后一个点之前的所有内容。 变量名称不区分大小写,只允许使用字母数字字符和 -,并且必须以字母字符开头。 有些变量可能会出现多次; 我们说这个变量是多值的。

语法相当灵活和宽松; 空格大多被忽略。 # 和 ; 字符开始注释到行尾,空白行被忽略。

该文件由部分(section)和变量组成。 一个部分以方括号中的部分名称开始,一直持续到下一个部分开始。 部分名称不区分大小写。 只有字母数字字符,- 和 . 允许在部分名称中。 每个变量必须属于某个部分(section),这意味着在第一次设置变量之前必须有部分(section)头。

部分可以进一步划分为小部分(section)。 要开始一个小部分(section),请将其名称放在双引号中,并在部分(section)标题中与部分(section)名用空格分隔,如下例所示:

 [section "subsection"]

小部分(section)名称区分大小写,可以包含除换行符和空字节以外的任何字符。 双引号 ” 和反斜杠可以通过将它们分别转义为 ” 和 \ 来包含。 阅读时删除其他字符前面的反斜杠; 例如,\t 读为 t,\0 读为 0。部分(section)标题不能跨越多行。 变量可以直接属于一个部分(section)或给定的子部分(section)。 如果你有 [section “subsection”],你可以有 [section],但你不需要。

所有其他行(以及部分(section)标题之后的行的其余部分)都被识别为设置变量,格式为 name = value(或只是 name,这是表示变量为布尔值”true”的简写形式 )。 变量名称不区分大小写,只允许使用字母数字字符和 -,并且必须以字母字符开头。如:

 [section "subsection"]  name = value
   name1 = value1
   name1 = value2

定义值的行可以通过以 \ 结束来继续到下一行; 反斜杠和行尾被剥离。 name = 之后的前导空格、第一个注释字符 # 或 ; 之后的行的其余部分以及行的尾随空格将被丢弃,除非它们用双引号引起来。 值中的内部空格会逐字保留。

在双引号内,必须对双引号 ” 和反斜杠 \ 字符进行转义:使用 ” 表示 “,使用 \ 表示 \。

[section]
    key = value \
    vvvvv ; comment
    key = "value1 \
    vvvvv222 ; comment"
    key = "value1   \\  \" "

$ git config --worktree --get-all section.key
value  vvvvv
value1  vvvvv222 ; comment
value1   \  "

可以识别以下转义序列(除了 ” 和 \):\n 表示换行符 (NL),\t 表示水平制表符 (HT、TAB) 和 \b 表示退格 (BS)。其他字符转义序列(包括八进制 转义序列)无效。

[section]
    key = value \n nextLine
    key = "value1\tTab "
    key = "value1\bBackspace"

$ git config --worktree --get-all section.key
value
 nextLine
value1  Tab
valueBackspace

Includes

您可以通过将特殊的 include.path(或 includeIf.*.path)变量设置为要包含的文件的名称来包含另一个配置文件。 该变量将路径名作为其值,并且受波浪号扩展的影响。 这些变量可以多次给出。

包含文件的内容会立即插入,就好像它们已在包含指令的位置找到一样。 如果变量的值是相对路径,则该路径被认为是相对于包含指令所在的配置文件的。

[include]
        path = /path/to/foo.inc # 使用绝对路径
        path = foo.inc # 使用相对路径
        path = ~/foo.inc #  find "foo.inc" in your $HOME directory/在用户目录中查找

Conditional includes

include 和 includeIf 部分允许您包含来自其他来源的配置指令。 这些部分的行为彼此相同,但如果 includeIf 部分的条件不为真,则可以忽略它们。 请参阅下面的”条件包含”。

您可以通过将 includeIf..path 变量设置为要包含的文件的名称来有条件地包含另一个配置文件。条件以关键字开头,后跟冒号和一些格式和含义取决于关键字的数据。 支持的关键字是:

关于通过 gitdir 和 gitdir/i 进行匹配的一些注意事项:

  • $GIT_DIR 中的符号链接在匹配之前不会被解析。
  • 路径的符号链接和真实路径版本都将在 $GIT_DIR 之外匹配。 例如。 如果 ~/git 是 /mnt/storage/git 的符号链接,则 gitdir:~/git 和 gitdir:/mnt/storage/git 都将匹配。
  • 在 v2.13.0 中此功能的初始版本并非如此,它仅匹配 realpath 版本。 想要与此功能的初始版本兼容的配置需要仅指定 realpath 版本,或同时指定两个版本。
  • 请注意,”../”并不特殊,并且会按字面意思匹配,这不太可能是您想要的。

Original: https://www.cnblogs.com/xysgo/p/16750021.html
Author: 菜阿
Title: Git命令列表–git-config

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/601283/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球