一、git config 命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
$ git config -h
usage: git config [<options>]

Config file location
--global use global config file
--system use system config file
--local use repository config file
--worktree use per-worktree config file
-f, --file <file> use given config file
--blob <blob-id> read config from given blob object

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
--remove-section remove a section: name
-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 <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

读取时,默认情况(不加 Config file location)从系统、全局和存储库配置文件中读取值,通过增加--system--global--local等选项来指示仅从指定位置进行读取。

写入时,默认情况(不加 Config file location)值会写入存储库配置文件中,通过增加--system--global--local等选项来指示将值写入到具体哪个配置文件中。

二、配置文件

配置文件位置

  • --system:使用系统配置文件
  • --global:使用全局配置文件
  • --local:使用仓库配置文件
  • --worktree:使用工作区级别的配置文件
  • -f,--file <文件>:使用指定的配置文件
  • --blob <数据对象ID>:从给定的数据对象读取配置

配置优先级

  • 优先级从低到高依次为:系统配置文件、全局配置文件、仓库配置文件、工作区配置文件
  • 按照上面的顺序读取配置文件,后面找到的值优先于之前读取的值。

1、系统级别配置文件

系统层面的配置文件,系统上所有用户都会读取该配置文件

  • 对于linux系统来说,位置在/etc/gitconfig
  • 对于windows系统来说,位置在git install dir/etc/gitconfig

2、全局级别配置文件

属于单个用户的配置文件,存储在用户的主目录下。

  • 对于linux系统来说,位置在~/.gitconfig~/.config/git/config
  • 对于windows系统来说,位置在C:/Users/user home dir/.gitconfig

3、仓库级别配置文件

针对不同的git仓库的配置文件。

位置在各个git仓库的.git/config

4、查看所有git配置及其来源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$ git config --list --show-origin
file:C:/Program Files/Git/etc/gitconfig diff.astextplain.textconv=astextplain
file:C:/Program Files/Git/etc/gitconfig filter.lfs.clean=git-lfs clean -- %f
file:C:/Program Files/Git/etc/gitconfig filter.lfs.smudge=git-lfs smudge -- %f
file:C:/Program Files/Git/etc/gitconfig filter.lfs.process=git-lfs filter-process
file:C:/Program Files/Git/etc/gitconfig filter.lfs.required=true
file:C:/Program Files/Git/etc/gitconfig http.sslbackend=openssl
file:C:/Program Files/Git/etc/gitconfig http.sslcainfo=C:/Program Files/Git/mingw64/etc/ssl/certs/ca-bundle.crt
file:C:/Program Files/Git/etc/gitconfig core.autocrlf=true
file:C:/Program Files/Git/etc/gitconfig core.fscache=true
file:C:/Program Files/Git/etc/gitconfig core.symlinks=false
file:C:/Program Files/Git/etc/gitconfig pull.rebase=false
file:C:/Program Files/Git/etc/gitconfig credential.helper=manager
file:C:/Program Files/Git/etc/gitconfig credential.https://dev.azure.com.usehttppath=true
file:C:/Program Files/Git/etc/gitconfig init.defaultbranch=master
file:C:/Users/z2huo/.gitconfig core.autocrlf=true
file:C:/Users/z2huo/.gitconfig core.quotepath=false
file:C:/Users/z2huo/.gitconfig core.ignorecase=false
file:C:/Users/z2huo/.gitconfig user.name=XXXXXX
file:C:/Users/z2huo/.gitconfig user.email=XXXXXX@163.com
file:C:/Users/z2huo/.gitconfig difftool.sourcetree.cmd=''
file:C:/Users/z2huo/.gitconfig mergetool.sourcetree.cmd=''
file:C:/Users/z2huo/.gitconfig mergetool.sourcetree.trustexitcode=true
file:C:/Users/z2huo/.gitconfig credential.https://gitee.com.provider=generic
file:.git/config core.repositoryformatversion=0
file:.git/config core.filemode=false
file:.git/config core.bare=false
file:.git/config core.logallrefupdates=true
file:.git/config core.symlinks=false
file:.git/config core.ignorecase=true
file:.git/config remote.origin.url=https://gitee.com/XXXXXX/XXX.git
file:.git/config remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
file:.git/config branch.master.remote=origin
file:.git/config branch.master.merge=refs/heads/master

三、配置一览

core.ignorecase

1
2
$ git config --get core.ignorecase
true

Git默认的配置为 false。

Windows:windows 系统默认不区分文件名称的大小写。windows 的默认文件系统为 NTFS,它本身是区分大小写的,但是 Windows 操作系统默认以不区分大小写的方式使用它。

Linux:Linux 系统通常是区分文件名称大小写的,Linux 使用的文件系统(如 Ext4、XFS、Btrfs等)是区分大小写的。

macOS:macOS 使用是 APFS(Apple File System)文件系统,专门为 macOS、IOS、tvOS、watchOS设计,于2016年首次发布,在 macOS 10.13 中首次默认使用。

默认为不区分大小写的模式。它可以是区分大小写的,也可以是不区分大小写的。在新建宗卷的时候可以看到可以选择格式,如下图所示:

core.autocrlf

Windows 使用回车(CR)和换行(LF)两个字符来结束一行,而 macOS 和 Linux 只使用换行(LF)一个字符。在跨平台协作时,这可能会有点问题。

Git 可以在提交时自动地把回车和换行转换为换行,而在检出时把换行转换为回车和换行。

1
$ git config --get core.autocrlf

该设置代表 Git 处理 \n\r 的方式:

  • 为 true 时,提交时将 \r\n 转换为 \n,拉取时将 \n 转换为 \r\n
  • 为 false 时,提交和拉取时都原样保留换行符
  • 为 input 时,提交时转成 \n,在拉取时原样保留换行符

因此,如果仅在 Windows 系统上进行开发,可以将该配置设置为 false,这样将保留回车在版本库中。

如果在 Windows 上进行开发,但是需要与使用 Linux 或 macOS 系统的开发者共享代码时,可以将该配置设置为 true,这样在代码仓库中存储的文件都使用统一的行结束符(LF),但在检出时会转换为 CRLF。

如果使用以换行作为行结束符的 Linux 或 macOS,不需要 Git 在拉取文件时进行自动转换,但是当一个以回车加换行作为行结束符的文件被引入时,但是在提交时需要 Git 对文件进行修正,可以将该配置设置为 input 来告诉 Git 在提交时将回车和换行转换为换行,检出时不转换。

core.safecrlf

1
2
$ git config --get core.safecrlf
true
  • 为true时,拒绝提交包含混合换行符的文字
  • 为false时,允许提交包含混合换行符的文字
  • 为warn时,提交包含混合换行符的文件时给出警告

core.quotepath

1
2
$ git config -- get core.quotepath
false

用于控制如何展示路径名。当git需要输出文件或目录的路径时,会根据quotepath的设置来决定是否对这些路径名进行转义。

转义过程为:将路径名用双引号括起来,将非打印控制字符和字节值值大于128(十进制)的字符进行转义,转义为8进制字符编码。例如:

1
2
3
4
5
6
7
8
9
10
$ git status
On branch master

No commits yet

Untracked files:
(use "git add <file>..." to include in what will be committed)
"\346\226\260\345\273\272 \346\226\207\346\234\254\346\226\207\346\241\243.txt"

nothing added to commit but untracked files present (use "git add" to track)

上面的文件名称为:新建 文本文档.txt

如果将该变量设置为false,那么字节值大于128的字符将不再被转义。

无论该变量设置如何,双引号、反斜杠和非打印控制字符总是要转义的。空格是不会被转义的。

更改quotepath之后,为了使中文正常显示,您可能还需要设置命令行界面使用中文或UTF-8编码,以确保中文字符可以正确显示。

相关链接

OB tags

#Git