目录

查看或修改git本地仓库或全局配置

git 的相关命令参数

配置文件作用域

属性操作

属性类型

其他

示例:

获取操作

         添加操作

修改操作

删除操作


查看或修改git本地仓库或全局配置

git在默认作用域下,如果本地仓库、全局或系统参数在获取或设置值时发生冲突,按如下的优先级获取或设置参数值。

git最终将读取所有的配置文件。

获取默认配置,如果当前地址中仓库信息不存在,则查看全局,然后再读取系统配置
git config --list

本地仓库配置 高优先级
git config --local --list


全局用户配置 中优先级
git config --global --list



系统配置 低优先级
git config --system --list

如果使用了诸如--local等命令,那么其作用域就将切换到--local。意味着此时无法获取或设置global和system的值。

通过上述命令可以查看config存储的一些配置参数

也可以通过直接查看文件的方式查看config

  •  系统config在安装目录下的/etc/gitconfig

  • 全局配置是用户目录中的.gitconfig文件

  • 仓库配置在项目.git/config


git 的相关命令参数

如下整理了一下相关的配置,每个使用到的配置,都会标注上自己的理解(待更新)

配置文件作用域

  • --local 使用本地仓库配置文件进行后续的操作命令
  • --global  使用全局配置文件进行后续的操作命令
  • --system 使用系统配置文件进行后续的操作命令
  • --worktree            use per-worktree config file
  • -f, --file <file>     use given config file
  • --blob <blob-id>      read config from given blob object

属性操作

  • --get  获取指定键的值(根据作用域,只返回第一个匹配到的值)
  • --get-all  获取键的所有值(读取所以配置文件的值)
  • --get-regexp          get values for regexp: name-regex [value-pattern]
  • --get-urlmatch        get value specific for the URL: section[.var] URL
  • --replace-all  替换键的值
  • --add 添加键值对
  • --unset  移除键值对,只有键时,将会删除该键值对,键后跟值时,代表仅删除改值
  • --unset-all           remove all matches: name [value-pattern]
  • --rename-section      rename section: old-name new-name
  • --remove-section      remove a section: name
  • -l, --list 展示所有参数
  • --fixed-value         use string equality when comparing values to 'value-pattern'
  • -e, --edit  打开编辑器
  • --get-color           find the color configured: slot [default]
  • --get-colorbool       find the color setting: slot [stdout-is-tty]

属性类型

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

其他

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

其他相关命令可以通过如下命令查看:

git config --help


示例:

--list 查看电脑的所有配置项

 git config --list

获取操作

  • 直接通过key value的方式获取值(总是从本地仓库开始获取参数值)
git config user.name

git config user.email

  • 通过get查看参数值(键不存在时,会返回空,同一文件下键有多个值时,取最后一个)
 git config --get core.bare

  •  --get-all查看所有值
git config --get-all user.name

添加操作

  • 添加(通过key-value形式)
git config user.name "newName"

当配置文件存在一个值对,该命令会覆盖键的值,;存在多个值对时,会由于找不到替换哪一个值对而报错

warning: user.name has multiple values
error: cannot overwrite multiple values with a single value
       Use a regexp, --add or --replace-all to change user.name.

  • 添加用户,邮箱(添加多个时,会产生相同的key以及其对应的值)
git config --add user.name "newNameLocal"

git config --add user.email "newNameLocal"

git config --global --add user.name "newNameGlobal"

git config --global --add user.email "newNameGlobal"

本地仓库配置文件

全局配置文件 

修改操作

  •  修改(通过key-value形式,同新增一样,当只有一个值对时,会覆盖,多个时会报错)
git config user.name "newName"

git config user.email "newName"
  • 修改全局配置(当只有一个用户名时,会覆盖,多个时会报错)
git config --global user.name "newName"

git config --global user.email "newName"
  •  替换(将所有键值对统一成一个)
git config --replace-all user.name "changeName"

删除操作

  • 删除键值对
 git config --unset core.newKey

  •  删除用户(删除键对应的值)
git config --unset user.name "newName"

git config --unset user.email "newName"

-edit 进入vim编辑器

git config -e

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐