git config基本篇

来源:互联网 发布:xshell mac 破解版 编辑:程序博客网 时间:2024/05/29 17:02
使用Git的第一件事就是设置你的名字和email,这些就是你在提交commit时的签名。
示例1
$ git config --global user.name "Robin Hu"
$ git config --global user.email "hudashi@gmail.com"
执行了上面的命令后,会在你的主目录(home directory)建立一个叫~/.gitconfig 的文件. 内容一般像下面这样:
[user]
name = Robin Hu
email = hudashi@gmail.com
git默认的编辑器是GNU nano这样的编辑器,我可以通过如下的命令把它设置为vim编辑器
示例2
git config --global core.editor vim
我们可以通过git config -h来获得git config命令的一些基本语法
 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
    -f, --file <file>     use given config file

Action
    --get                 get value: name [value-regex]
    --get-all             get all values: key [value-regex]
    --get-regexp          get values for regexp: name-regex [value-regex]
    --replace-all         replace all matching variables: name value [value_regex]
    --add                 adds a new variable: name value
    --unset               removes a variable: name [value-regex]
    --unset-all           removes all matches: name [value-regex]
    --rename-section      rename section: old-name new-name
    --remove-section      remove a section: name
    -l, --list            list all
    -e, --edit            opens an editor
    --get-color <slot>    find the color configured: [default]
    --get-colorbool <slot>
                          find the color setting: [stdout-is-tty]

Type
    --bool                value is "true" or "false"
    --int                 value is decimal number
    --bool-or-int         value is --bool or --int
    --path                value is a path (file or directory name)

Other
    -z, --null            terminate values with NUL byte
   
Git 使用一系列的配置文件来存储你定义的偏好,它首先会查找/etc/gitconfig文件,该文件含有 对系统上所有用户及他们所拥有的仓库都生效的配置值, 如果传递--system选项给git config命令, Git 会读写这个文件。
接下来 Git 会查找每个用户的~/.gitconfig文件,你能传递--global选项让 Git读写该文件。
最后 Git 会查找由用户定义的各个库中 Git 目录下的配置文件(.git/config),该文件中的值只对该git库有效。
 如果传递 --local 选项给git config命令, Git 会读写这个文件。 --local  选项是默认选项
 以上阐述的三层配置从一般到特殊层层推进,如果定义的值有冲突,以后面层中定义的为准,例如:在.git/config和/etc/gitconfig的较量中, .git/config取得了胜利。虽然你也可以直接手动编辑这些配置文件,但是运行git config命令将会来得简单些。
-l, --list 用于列出我们已经设置了的git的配置信息。在
示例3
 git config --global -l
user.name=Robin Hu
user.email=hudashi@gmail.com

关于git config的更多内容请参考《git config高级篇》
原创粉丝点击