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

Shell

描述

可以使用此命令查询/设置/替换/取消设置选项。该名称实际上是由点(.)分隔键,该值将被转义。

可以使用--add选项将多行添加到选项。如果要更新或取消设置多行可能出现的选项,则需要给出POSIX正则表达式value_regex。 只有与正则表达式匹配的现有值已更新或未设置。如果要处理与正则表达式不匹配的行,只需在前面添加一个感叹号。

类型说明符可以是--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

一. 配置文件的存储位置

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

  1. /etc/gitconfig 文件:包含了适用于系统所有用户和所有库的值。如果你传递参数选项’—system’ 给 git config,它将明确的读和写这个文件。

  2. ~/.gitconfig 文件 :具体到你的用户。你可以通过传递—global 选项使Git 读或写这个特定的文件。

  3. 位于git目录的config文件 (也就是 .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"

Shell

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

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

Shell

三.配置编缉器

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

$ git config --global core.editor emacs

Shell

四.配置比较工具

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

$ git config --global merge.tool vimdiff

Shell

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

Shell

可能会看到一个关键字出现多次(如这里的: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

Shell

添加/删除配置项

添加配置项

参数

  • -–add

格式: git config [–local|–global|–system] –add section.key value(默认是添加在 local 配置中)

注意add后面的 section, key, value 一项都不能少,否则添加失败。比如执行:

$ git config -–add site.name yiibai

Shell

删除配置项
命令参数

  • -–unset
    格式:git config [–local|–global|–system] –unset section.key
    相信有了前两个命令的使用基础,大家举一反三就知道改怎么用了,现在试试删除 local 配置中的site.name配置值 -
$ git config --local -–unset site.name

Shell

六.获取帮助

如果在使用Git时需要帮助,有三种方法可以获得任何git命令的手册页(manpage)帮助信息:

$ git help <verb>
$ git <verb> --help
$ man git-<verb>

Shell

例如,您想要看看有关 git config 如何使用,那么可以使用以下命令 -

$ git help config

  • 21
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智慧浩海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值