首页·终端命令

git config

git

用法

git config [--global] <key> [<value>]

读写 git 的配置;不加 --global 只写进当前仓库,重新克隆一份就没有这条设置了。

常用选项

选项含义
--globalwrite to ~/.gitconfig for every repository
--localwrite to .git/config, the default
--list --show-originprint every setting and the file it came from
--get <key>print one value
--unset <key>delete one setting
-eopen the config file in your editor

示例

git config --global user.email "me@example.com"

sets the address every future commit is signed with

git config --global init.defaultBranch main

new repositories start on main instead of master

git config --list --show-origin

shows which file each setting comes from

难的地方在于撤销有好几种:把分支的指向挪走,和补一个反向提交,不是一回事。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. git config 是做什么的?

读写 git 的配置;不加 --global 只写进当前仓库,重新克隆一份就没有这条设置了。

Q. 怎么写?

git config [--global] <key> [<value>] —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 6 个,完整列表在 man git。这条命令属于git。

相关命令

man 手册: man git-config