Git命令列表--git-config

Git Config

名称

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

配置文件的范围和语法

$ git config 
usage: it config [<options>]

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 <file>     use given config file/指定(特定)的配置文件
    --blob <blob-id>      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 <value>     with --get, use default value when missing entry

简单命令示例

环境准备:

  1. 新建一个空的目录 /d/VSCode/testGit

  2. 初始化git项目 git init

    $ git init  
    Initialized empty Git repository in D:/VSCode/testGit/.git/
    
  3. 添加工作树 git worktree add wtree

    $ git worktree add wtree
    Preparing worktree (checking out 'wtree')
    HEAD is now at e6dde55 first
    
    $ git worktree list
    D:/VSCode/testGit        e6dde55 [master]
    D:/VSCode/testGit/wtree  e6dde55 [wtree] 
    
    $ git worktree add ../wt2
    Preparing worktree (new branch 'wt2')
    HEAD is now at e6dde55 first
    
    $ git worktree list                                                                
    D:/VSCode/testGit        e6dde55 [master]
    D:/VSCode/testGit/wtree  e6dde55 [wtree] 
    D:/VSCode/wt2            e6dde55 [wt2]   
    
  4. 添加配置

    为了具有特定于工作树的配置,您可以打开 worktreeConfig 扩展

    ### 如果没有添加配置会出现如下错误
    user@NAME MINGW64 /d/VSCode/wt2 (wt2)
    $ git config --worktree c.a va
    fatal: --worktree cannot be used with multiple working trees unless the config
    extension worktreeConfig is enabled. Please read "CONFIGURATION FILE"
    section in "git help worktree" for details
    
    git config extensions.worktreeConfig true
    

配置命令示例:

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

添加worktree范围配置 --worktree

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

获取配置(多值) --get/--get-all

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

添加配置2

user@NAME MINGW64 /d/VSCode/wt2 (wt2)
git config --worktree c.b  valb
git config --worktree c.c valc

配置列表 --list/-l

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

删除配置 --unset

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

## --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

重命名键的部分(section)名称(相当于重新命名一组前缀相同的配置) --rename-section

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	

删除键的部分(section)(相当于删除一组前缀相同的配置) --remove-section

## 先添加几个配置组 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

Url匹配 --get-urlmatch

# 配置文件内容
[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 [<file-option>] [--type=<type>] [--fixed-value] [--show-origin] [--show-scope] [-z|--null] name [value [value-pattern]]
git config [<file-option>] [--type=<type>] --add name value
git config [<file-option>] [--type=<type>] [--fixed-value] --replace-all name value [value-pattern]
git config [<file-option>] [--type=<type>] [--show-origin] [--show-scope] [-z|--null] [--fixed-value] --get name [value-pattern]
git config [<file-option>] [--type=<type>] [--show-origin] [--show-scope] [-z|--null] [--fixed-value] --get-all name [value-pattern]
git config [<file-option>] [--type=<type>] [--show-origin] [--show-scope] [-z|--null] [--fixed-value] [--name-only] --get-regexp name_regex [value-pattern]
git config [<file-option>] [--type=<type>] [-z|--null] --get-urlmatch name URL
git config [<file-option>] [--fixed-value] --unset name [value-pattern]
git config [<file-option>] [--fixed-value] --unset-all name [value-pattern]
git config [<file-option>] --rename-section old_name new_name
git config [<file-option>] --remove-section name
git config [<file-option>] [--show-origin] [--show-scope] [-z|--null] [--name-only] -l | --list
git config [<file-option>] --get-color name [default]
git config [<file-option>] --get-colorbool name [stdout-is-tty]
git config [<file-option>] -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 选项时有效。

配置文件范围 file-option

准备配置文件

  1. 在git库中添加一个配置文件 blob.config,内容如下

    [blob]
    	repositoryformatversion = 0
    	filemode = false
    	bare = false
    	logallrefupdates = true
    	symlinks = false
    	ignorecase = true
    
  2. 提交后,找到配置文件的blob-id

    xuyuansheng@XUYUANSHENG MINGW64 /d/VSCode/testGit (master)
    $ git log 
    commit c0607d30d05ee1e2a35735a42e88d16d61d05388 (HEAD -> master)
    Author: yuansheng.xu <804288658@qq.com>
    Date:   Sun Oct 9 13:59:55 2022 +0800
    
        添加git配置文件到版本管理中
        
    $ git cat-file -p c0607d30d05ee1e2a35735a42e88d16d61d05388 
    tree 087aa3a41f2abccf7573e9555ec22072e16225c8
    parent e6dde55e8c5b5f99b9d43b0793ea0f12cc41bf41
    author yuansheng.xu <804288658@qq.com> 1665295195 +0800   
    committer yuansheng.xu <804288658@qq.com> 1665295195 +0800
    
    添加git配置文件到版本管理中
    
    $ git cat-file -p 087aa3a41f2abccf7573e9555ec22072e16225c8
    100644 blob 890ad307962dd370f70c3f8d3af180c9c59a909f    blob.config
    100644 blob d0e51f9de5892fa7978357498ae79bb11bbe9459    copy.sh    
    100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391    fff.txt
    
    ## 890ad307962dd370f70c3f8d3af180c9c59a909f 这个即为blob.config的Id
        
    

验证配置

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

配置的值类型 --type

## 无论是获取或者修改(添加)配置 都会校验值的类型	
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'

值正则表达式 value-pattern

## 配置文件内容
$ 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 变量设置为要包含的文件的名称来有条件地包含另一个配置文件。条件以关键字开头,后跟冒号和一些格式和含义取决于关键字的数据。 支持的关键字是:

  1. gitdir

    关键字 gitdir: 后面的数据用作 glob 模式。 如果 .git 目录的位置与模式匹配,则满足包含条件。.git 位置可能是自动发现的,或者来自 $GIT_DIR 环境变量。 如果存储库是通过 .git 文件(例如从子模块或链接的工作树)自动发现的,则 .git 位置将是 .git 目录所在的最终位置,而不是 .git 文件所在的位置。该模式可以包含标准的通配符和两个附加的通配符,/ 和 /,它们可以匹配多个路径组件。 详情请参考 gitignore(5)。 为了方便:

    • 如果模式以 ~/ 开头,则 ~ 将替换为环境变量 HOME 的内容。
    • 如果模式以 ./ 开头,则替换为包含当前配置文件的目录。
    • 如果模式不是以 ~/、./ 或 / 开头,则 **/ 将自动添加到前面。 例如,模式 foo/bar 变为 **/foo/bar 并且将匹配 /any/path/to/foo/bar。
    • 如果模式以 / 结尾,则会自动添加 。 例如,模式 foo/ 变为 foo/。 换句话说,它递归地匹配“foo”和里面的所有东西。
    ; include if $GIT_DIR is /path/to/foo/.git
    [includeIf "gitdir:/path/to/foo/.git"]
            path = /path/to/foo.inc
    
    ; include for all repositories inside /path/to/group
    [includeIf "gitdir:/path/to/group/"]
            path = /path/to/foo.inc
    
    ; include for all repositories inside $HOME/to/group
    [includeIf "gitdir:~/to/group/"]
            path = /path/to/foo.inc
    
    ; relative paths are always relative to the including
    ; file (if the condition is true); their location is not
    ; affected by the condition
    [includeIf "gitdir:/path/to/group/"]
            path = foo.inc
    
  2. gitdir/i

    这与 gitdir 相同,只是匹配不区分大小写(例如,在不区分大小写的文件系统上)

  3. onbranch

    关键字 onbranch: 后面的数据被视为具有标准通配符和两个附加通配符的模式,/ 和 /,可以匹配多个路径组件。 如果我们在当前签出的分支名称与模式匹配的工作树中,则满足包含条件。

    如果模式以 / 结尾,则会自动添加 。 例如,模式 foo/ 变为 foo/。 换句话说,它匹配所有以 foo/ 开头的分支。 如果您的分支是分层组织的,并且您希望将配置应用于该层次结构中的所有分支,这将非常有用。

    ; include only if we are in a worktree where foo-branch is
    ; currently checked out
    [includeIf "onbranch:foo-branch"]
            path = foo.inc
    

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

  • $GIT_DIR 中的符号链接在匹配之前不会被解析。
  • 路径的符号链接和真实路径版本都将在 $GIT_DIR 之外匹配。 例如。 如果 ~/git 是 /mnt/storage/git 的符号链接,则 gitdir:~/git 和 gitdir:/mnt/storage/git 都将匹配。
  • 在 v2.13.0 中此功能的初始版本并非如此,它仅匹配 realpath 版本。 想要与此功能的初始版本兼容的配置需要仅指定 realpath 版本,或同时指定两个版本。
  • 请注意,“../”并不特殊,并且会按字面意思匹配,这不太可能是您想要的。
posted @ 2022-10-03 09:21  菜阿  阅读(381)  评论(0编辑  收藏  举报