[TOC] #### 1. 命令介绍 --- `git config` 用于查看和设置 git 的各种配置项,只要你用 git,就离不开这个命令 #### 2. 查看配置 --- 查看当前所有配置信息,列出所有层级的配置信息,包括: + 系统配置(存储在 `/etc/gitconfig` 文件中,用的比较少) + 全局配置(存储在 `~/.gitconfig` 文件中) + 本地仓库配置(存储在当前仓库 `.git/config` 文件中) ```bash # --list 可简写为 -l git config --list ``` 查看所有配置的同时,还想要查看每个配置项所在的文件位置,可以使用: ```bash # 这个命令会输出每个配置项及其对应的配置文件路径 git config --list --show-origin ``` 查看指定层级的所有配置 ```bash # 查看系统配置 git config --system --list # 查看全局配置 git config --global --list # 查看本地仓库配置 git config --local --list ``` 也可以直接查看配置文件内容 ```bash # 查看全局配置 cat ~/.gitconfig # 查看当前仓库配置(需要在 .git 所在目录执行) cat .git/config ``` 查看某个配置项,可以使用: ```bash git config user.name git config user.email ``` 如果多个配置层级设置了相同的配置项,那么会使用优先级较高的层级中的配置,优先级由高到低依次为: ```plaintext 本地仓库配置(.git/config) > 全局配置(~/.gitconfig) > 系统配置(/etc/gitconfig) ``` #### 3. 修改配置 --- 全局配置:用于设置影响所有 git 仓库的全局配置项,是我们最常用的命令之一 ```plaintext git config --global <key> <value> ``` 使用示例: ```bash # 这里的用户名和邮箱只是用在提交记录中显示,和是否有权限提交代码无关,这是很多新手的误区 git config --global user.name "liang" git config --global user.emial "23426945@qq.com" ``` 如果只想修改当前仓库的某些配置项,其他仓库不受影响,进入当前仓库,允许以下命令: ```bash # --local 是可以省略的 git config --local <key> <value> ``` #### 4. 删除配置 --- 删除配置项,语法格式: + scopes:配置范围,system / global / local + key:要删除的配置项 ```plaintext git config <scopes> --unset <key> ``` 使用示例: ```bash git config --local --unset user.name git config --global --unset user.name git config --system --unset user.name ```