• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • git config

    git config:命令用于获取并设置存储库或全局选项。这些变量可以控制 Git 的外观和操作的各个方面。

    语法

    git config [<file-option>] [type] [--show-origin] [-z|--null] name [value [value_regex]]
    git config [<file-option>] [type] --add name value
    git config [<file-option>] [type] --replace-all name value [value_regex]
    git config [<file-option>] [type] [--show-origin] [-z|--null] --get name [value_regex]
    git config [<file-option>] [type] [--show-origin] [-z|--null] --get-all name [value_regex]
    git config [<file-option>] [type] [--show-origin] [-z|--null] [--name-only] --get-regexp name_regex [value_regex]
    git config [<file-option>] [type] [-z|--null] --get-urlmatch name URL
    git config [<file-option>] --unset name [value_regex]
    git config [<file-option>] --unset-all name [value_regex]
    git config [<file-option>] --rename-section old_name new_name
    git config [<file-option>] --remove-section name
    git config [<file-option>] [--show-origin] [-z|--null] [--name-only] -l | --list
    git config [<file-option>] --get-color name [default]
    git config [<file-option>] --get-colorbool name [stdout-is-tty]
    git config [<file-option>] -e | --edit
    
    • 可以使用此命令查询/设置/替换/取消设置选项。该名称实际上是由点(.)分隔键,该值将被转义。
    • 可以使用--add选项将多行添加到选项。如果要更新或取消设置多行可能出现的选项,则需要给出 POSIX 正则表达式value_regex。 只有与正则表达式匹配的现有值已更新或未设置。如果要处理与正则表达式不匹配的行,只需在前面添加一个感叹号(makr>!)。
    • 类型说明符可以是--int--bool,以使git config确保变量是给定类型,并将该值转换为规范形式(int 是简单十进制数,“true”或“false”的布尔字符串表示),或者--path,它进行一些路径扩展。如果没有类型说明符传递,则不会对该值执行检查或转换。
    • 读取时,默认情况下从系统,全局和存储库本地配置文件读取这些值,而选项--system--global--local--file <filename>可用于告知命令从只有那个位置设置和读取。
    • 写入时,默认情况下将新值写入存储库本地配置文件,并且可以使用选项--system--global--file <filename>来告诉命令写入该位置(可以指定为--local,但这是默认值)。

    此错误后,此命令将失败,并显示非零状态。一些退出代码是:

    • 部分或键无效(ret = 1)。
    • 没有提供部分或名称(ret = 2)。
    • 配置文件无效(ret = 3)。
    • 配置文件无法写入(ret = 4)。
    • 尝试取消设置不存在的选项(ret = 5)。
    • 尝试取消设置/设置多个行匹配的选项(ret = 5)或尝试使用无效的正则表达式(ret = 6)。

    成功后,命令返回退出代码是:0 。


    配置文件的存储位置

    这些变量可以被存储在三个不同的位置:

    • /etc/gitconfig文件:包含了适用于系统所有用户和所有库的值。如果你传递参数选项’—system’给 git config,它将明确的读和写这个文件。
    • ~/.gitconfig文件:具体到你的用户。你可以通过传递—global选项使 Git 读或写这个特定的文件。
    • .git/config:无论当前在用的库是什么,特定指向该单一的库。每个级别重写前一个级别的值。因此,在.git/config中的值覆盖了在/etc/gitconfig中的同一个值。


    配置用户名和密码

    当安装 Git 后首先要做的事情是设置用户名称和 e-mail 地址。这是非常重要的,因为每次 Git 提交都会使用该信息。它被永远的嵌入到了你的提交中:

    git config --global user.name "maxsu"
    git config --global user.email "yiibai.com@gmail.com"
    

    重申一遍,只需要做一次这个设置。如果传递了--global选项,因为 Git 将总是会使用该信息来处理在系统中所做的一切操作。如果希望在一个特定的项目中使用不同的名称或 e-mail 地址,可以在该项目中运行该命令而不要--global选项。

    git config  user.name "maxsu"
    git config user.email "yiibai.com@gmail.com"
    


    配置编缉器

    可以配置默认的文本编辑器,Git 在需要你输入一些消息时会使用该文本编辑器。缺省情况下,Git 使用系统的缺省编辑器,这通常可能是 vi 或者 vim 。如果想使用一个不同的文本编辑器,例如:Emacs,可以按照如下操作:

    git config --global core.editor emacs
    


    配置比较工具

    另外一个你可能需要配置的有用的选项是缺省的比较工具它用来解决合并时的冲突。例如,想使用 vimdiff 作为比较工具:

    git config --global merge.tool vimdiff
    

    Git 可以接受 kdiff3, tkdiff, meld, xxdiff, emerge, vimdiff, gvimdiff, ecmerge,和 opendiff 作为有效的合并工具。也可以设置一个客户端的工具。


    检查配置

    如果想检查你的设置,可以使用git config --list命令来列出 Git 可以在该处找到的所有的设置:

    git config --list
    core.symlinks=false
    core.autocrlf=true
    color.diff=auto
    color.status=auto
    color.branch=auto
    color.interactive=true
    pack.packsizelimit=2g
    help.format=html
    http.sslcainfo=/bin/curl-ca-bundle.crt
    sendemail.smtpserver=/bin/msmtp.exe
    diff.astextplain.textconv=astextplain
    rebase.autosquash=true
    user.email=yiibai.com@gmail.com
    user.name=minsu
    user.author=author-maxsu
    core.autocrlf=true
    core.repositoryformatversion=0
    core.filemode=false
    core.bare=false
    core.logallrefupdates=true
    core.symlinks=false
    core.ignorecase=true
    core.hidedotfiles=dotGitOnly
    gui.wmstate=normal
    gui.geometry=799x475+304+80 287 214
    user.name=maxsu
    

    可能会看到一个关键字出现多次(如这里的:user.name 就有两个值),这是因为 Git 从不同的文件中(例如:/etc/gitconfig以及~/.gitconfig)读取相同的关键字。在这种情况下,对每个唯一的关键字,Git 使用最后的那个值。也可以查看 Git 认为的一个特定的关键字目前的值,使用如下命令 git config{key}:

    git config user.name
    maxsu
    
    Administrator@MY-PC /C/Users/Administrator/Desktop (master)
    
    git config user.email
    your_email@mail.com
    


    添加置项

    git config [–local|–global|–system] –add section.key value()
    

    默认是添加在 local 配置中。section.keyvalue一项都不能少,否则添加失败。


    删除配置项

    git config [–local|–global|–system] –unset section.key
    

    例如,删除 local 配置中的 site.name 配置值:

    git config --local -–unset site.name
    

    下篇:git init