git config配置Git (二)

使用git config配置Git

1 git config 概要

git config [<file-option>] [--type=<type>] [--show-origin] [-z|--null] name [value [value_regex]]
git config [<file-option>] [--type=<type>] --add name value
git config [<file-option>] [--type=<type>] --replace-all name value [value_regex]
git config [<file-option>] [--type=<type>] [--show-origin] [-z|--null] --get name [value_regex]
git config [<file-option>] [--type=<type>] [--show-origin] [-z|--null] --get-all name [value_regex]
git config [<file-option>] [--type=<type>] [--show-origin] [-z|--null] [--name-only] --get-regexp name_regex [value_regex]
git config [<file-option>] [--type=<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

这个命令可以查询/设置/替换/取消设置配置选项。
读取时,默认情况下从系统,全局,本地存储库配置文件中读取配置,--system,--global,--local,--worktree,--file <filename>用于指定读取的位置。写入时同理。

  • [--replace-all]
    默认是替换一行。这个可以匹配所有行去替换键值。

  • [--add]
    在不改变任何已有的键值情况下添加一行新键值。即便配置已存在也会再创建一个新键值。请添加图片描述

  • [--get]
    获取键值,如果不存在返回错误代码1.

  • [--get-all]
    获取所有匹配的键值,如果不存在返回错误代码1.

  • [--get-regexp]
    类似于--get-all获取匹配的键值,不过可以使用正则表示来匹配。

  • [--get-urlmatch name URL]
    默认配置是使用<section>.<url>.<key>来标注的,<url>可能不存在,此表达式首先使用<name>匹配<section>,其中<url>如果最匹配则返回键值,否则返回匹配<section>的列表,如果没有找到则返回错误代码1。

  • [--system]
    指定从系统中读写配置文件,这个文件的位置在Linux上默认是/etc/gitconfig

  • [--global]
    指定从全局读写配置文件,这个文件位置在Linux上默认是~/.gitconfig

  • [--local]
    指定从Git存储库读写配置文件,这个文件位置在.git/config文件中。

  • [--worktree]
    类似于--local,默认是在.git/config.worktree文件中,只有在extensions.worktreeConfig配置后生效,否则该选项就是--local

  • [-f config-file,--file config-file]
    从指定文件读写配置文件

  • [--blob blob]
    类似于--file不过是从二进制文件读写。感兴趣可以研究该用法。

  • [--remove-section]
    移除所有给定<setction>配置

  • [--rename-section]
    重命名给定<section>一个新名字,如:

    core.xxxx -> newname.xxxx
    
  • [--unset]
    移除一个匹配的键值

  • [--unset-all]
    移除所有匹配的键值

  • [-l, --list]
    列出所有当前已有的配置变量。

  • [--type <type>]
    确保读写的配置变量符合<type>类型。可能的<type>如下:

    • booltrue或者false
    • int:如123,可选后缀kmg
    • bool-or-int
    • path
    • expiry-date
    • color

    当然也可以直接这样使用--bool--int--path等。

  • [--no-type]
    如上

  • [-z, --null]
    输出的键值是以null字符结束,而不是换行符,可用于对输出的安全解析。

  • [--name-only]
    对于--list或者--get-regexp仅输出匹配名字的配置变量。

  • [--show-origin]
    不仅输出配置,还会输出对应配置的来源文件。

  • [--get-colorbool name [stdout-is-tty]][--get-color name [default]]
    省略

  • [-e, --edit]
    打开一个编辑器去修改配置文件。

  • [--[no-]includes]
    省略

  • [--default <value>]
    当使用--get查找配置变量时,如果对应键值不存在,则默认写入键值<value>

2 示例

一般刚刚安装好GIT,需要配置用户名和邮箱,如下:

$ git config --global user.name "Hengtian Zhang"
$ git config --global user.email hengtian.zhang@foxmail.com

在Linux上,首先Git会查找系统级/etc/gitconfig文件,该文件包含系统里每位用户及他们所拥有仓库的配置变量,如果传递--system选项给git config则会读写该文件。
接着Git会查找每位用户的~/.gitconfig文件(或者~/.config/git/config)。可以传递--global选项读写该文件。
最后是查找当前正在的Git仓库下的.git/config文件,该文件只对该仓库有效,可以传递--local读写该文件。
以上三个层次的配置都会覆盖掉上一层的配置。
我们也可以直接编辑对应文件达到修改配置效果,如下:

[user]
    email = hengtian.zhang@foxmail.com
    name = Hengtian Zhang

[user]中的内容对应配置变量的<section>
email和name对应配置变量中的<key>
格式如下<section>.<key> <value>

3 一些有用的配置变量

  • core.editor
    默认情况下git会通过环境变量$VISUAL$EDITOR启动文本编辑器,如果没有设置则默认使用vi来启动,使用配置变量core.editor来修改默认启动的编辑器:

    $ git config --global core.editor emacs
    

    现在无论定义了什么终端编辑器,Git都会调用配置的emacs编辑器。
    下面是一些编辑器的配置:

    编辑器设置命令
    Atomgit config --global core.editor "atom --wait"
    BBEditgit config --global core.editor "bbedit -w"
    Emacsgit config --global core.editor emacs
    Gedit (Linux)git config --global core.editor "gedit --wait --new -window
    Gvim (Windows 64-bit)git config --global core.editor "'C:/Program Files/Vim/vim72/gvim.exe' --nofork '%*'" (Also see note below)
    Kate (Linux)git config --global core.editor "kate"
    nanogit config --global core.editor "nano -w"
    Notepad (Windows 64-bit)git config core.editor notepad
    Notepad++ (Windows 64-bit)git config --global core.editor "atom --wait"
    Vimgit config --global core.editor "vim"
    VS Codegit config --global core.editor "code --wait"
  • commit.template
    如果把这个配置变量设置为系统上的某个文件,当提交的时候,Git会使用该文件的内容作为默认提交初始化信息。在协同合作中,该提交模板可以用来规范自己和他人的提交格式和风格:
    如有一个~/.gitmessage.txt模板文件:

    $ git config --global commit.template ~/.gitmessage.txt
    $ git commit
    

    则会在编辑器中出现模板中的提交信息占位符。

  • core.pager
    当使用logdiff命令时默认会使用分页器,可以把这个设置其他选项来配置分页器:

    $ git config --global core.pager more  # 每一页显示更多信息
    $ git config --global core.pager less  # 每一页显示更少信息
    $ git config --global core.pager ''    # 全部显示
    
  • user.signingkey
    如果要创建经签署含附注的标签,那么可以把GPG签署密匙设置为配置变量:

    $ git config --global user.signingkey <gpg-key-id>
    

    这样每次运行git tag就可以直接进行签署为不需要密匙:

    $ git tag -s <tag-name>
    
  • core.excludesfile
    此选项可以使用一个类似与.gitignore文件,不过它会全局生效,不仅仅在当前Git存储库,如有这样一个文件~/.gitignore_global

    *~
    .*.swp
    .DS_Store
    

    执行git config --global core.excludesfile ~/.gitignore_global则Git会全局忽略掉这些文件。

  • help.autocorrect
    一般当敲错命令,会显示:

    $ git: 'checkou' is not a git command. See 'git --help'.
    
    The most similar command is
        checkout
    

    Git会尝试猜测你的意图,如果help.autocorrect设置成 1,则如果输出命令,Git模糊匹配到了一个命令则Git会直接执行该命令,而不是询问你。
    注意1表示十分之一的时间,用于延迟这么多时间后执行,所以如果设置成50则会延迟5秒执行命令,足够让你改变主意。

  • core.*
    着色相关的配置都被忽略不做解释。

  • merge.tool
    用于配置一个合并工具,当执行git mergetool命令时会自动启动设置的合并工具。

  • diff.external
    用于配置一个比较工具,当执行git difftool命令时会自动启动设置的比较工具。

  • core.autocrlf
    假如你正在 Windows 上写程序,而你的同伴用的是其他系统(或相反),你可能会遇到 CRLF 问题。 这是因为 Windows 使用回车(CR)和换行(LF)两个字符来结束一行,而 macOS 和 Linux 只使用换行(LF)一个字 符。 虽然这是小问题,但它会极大地扰乱跨平台协作。许多 Windows 上的编辑器会悄悄把行尾的换行字符转换成回车和换行, 或在用户按下 Enter 键时,插入回车和换行两个字符。
    Git 可以在你提交时自动地把回车和换行转换成换行,而在检出代码时把换行转换成回车和换行。 你可以用core.autocrlf 来打开此项功能。 如果是在 Windows 系统上,把它设置成 true,这样在检出代码时,换行会被转换成回车和换行:

    $ git config --global core.autocrlf true
    

    如果使用以换行作为行结束符的 Linux 或 macOS,你不需要 Git 在检出文件时进行自动的转换; 然而当一个以回车加换行作为行结束符的文件不小心被引入时,你肯定想让 Git 修正。 你可以把 core.autocrlf 设置成input 来告诉 Git 在提交时把回车和换行转换成换行,检出时不转换:

    $ git config --global core.autocrlf input
    

    这样在 Windows 上的检出文件中会保留回车和换行,而在 macOS 和 Linux 上,以及版本库中会保留 换行。 如果你是 Windows 程序员,且正在开发仅运行在 Windows 上的项目,可以设置 false 取消此功能,把回车保 留在版本库中:

    $ git config --global core.autocrlf false
    
  • core.whitespace
    Git 预先设置了一些选项来探测和修正多余空白字符问题。 它提供了六种处理多余空白字符的主要选项 —— 其中三个默认开启,另外三个默认关闭,不过你可以自由地设置它们。
    默认被打开的三个选项是:blank-at-eol,查找行尾的空格;blank-at-eof,盯住文件底部的空行;space-before-tab,警惕行头 tab 前面的空格。
    默认被关闭的三个选项是:indent-with-non-tab,揪出以空格而非 tab 开头的行(你可以用 tabwidth 选
    项控制它);tab-in-indent,监视在行头表示缩进的 tab;cr-at-eol,告诉 Git 忽略行尾的回车。 通过设置 core.whitespace,你可以让 Git 按照你的意图来打开或关闭以逗号分割的选项。 要想关闭某个选 项,你可以在输入设置选项时不指定它或在它前面加个 -。 例如,如果你想要打开除space-before-tab 之外
    的所有选项,那么可以这样 ( trailing-space 涵盖了 blank-at-eolblank-at-eof):

    $ git config --global core.whitespace \
          trailing-space,-space-before-tab,indent-with-non-tab,tab-in-indent,crat-eol
    

    你也可以只指定自定义的部分:

    $ git config --global core.whitespace \
         -space-before-tab,indent-with-non-tab,tab-in-indent,cr-at-eol
    

    当你运行 git diff 命令并尝试给输出着色时,Git 将探测到这些问题,因此你在提交前就能修复它们。 用 git apply 打补丁时你也会从中受益。 如果正准备应用的补丁存有特定的空白问题,你可以让 Git 在应用补丁时发出警告:

    $ git apply --whitespace=warn <patch>
    

    或者让 Git 在打上补丁前自动修正此问题:

    $ git apply --whitespace=fix <patch>
    

    这些选项也能运用于 git rebase。 如果提交了有空白问题的文件,但还没推送到上游,你可以运行 git rebase --whitespace=fix 来让 Git 在重写补丁时自动修正它们。

  • alias.xxxx
    配置一个别名,当执行的命令是一个别名是会自动执行别名:

    $ git config --global alias.lg 'log --graph --oneline --abbrev-commit'
    $ git lg
    *   a8ad9a2434dc - (HEAD -> main, origin/master, origin/HEAD, test_linux, test_dev, master) Merge tag 'efi-urgent-for-v5.16-2' of git://git.kernel.org/pub/scm/linux/kernel/git/efi/efi (3 weeks ago) <Linus Torvalds>
    |\  
    | * 4bc5e64e6cf3 - efi: Move efifb_setup_from_dmi() prototype from arch headers (5 weeks ago) <Javier Martinez Canillas>
    * | fc74e0a40e4f - Linux 5.16-rc7 (3 weeks ago) <Linus Torvalds>
    * |   e8ffcd3ab0e5 - Merge tag 'x86_urgent_for_v5.16_rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip (3 weeks ago) <Linus Torvalds>
    |\ \  
    | * | 57690554abe1 - x86/pkey: Fix undefined behaviour with PKRU_WD_BIT (4 weeks ago) <Andrew Cooper>
    | * | 2f5b3514c33f - x86/boot: Move EFI range reservation after cmdline parsing (5 weeks ago) <Mike Rapoport>
    | * | fbe618399854 - Revert "x86/boot: Pull up cmdline preparation and early param parsing" (5 weeks ago) <Borislav Petkov>
    | * | 58e138d62476 - Revert "x86/boot: Mark prepare_command_line() __init" (5 weeks ago) <Borislav Petkov>
    * | |   2afa90bd1c75 - Merge tag 'objtool_urgent_for_v5.16_rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip (3 weeks ago) <Linus Torvalds>
    |\ \ \  
    | * | | dcce50e6cc4d - compiler.h: Fix annotation macro misplacement with Clang (4 weeks ago) <Josh Poimboeuf>
    | * | | cb8747b7d2a9 - uapi: Fix undefined __always_inline on non-glibc systems (4 weeks ago) <Ismael Luceno>
    * | | |   438645193e59 - Merge tag 'pinctrl-v5.16-3' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-pinctrl (3 weeks ago) <Linus Torvalds>
    |\ \ \ \  
    | * | | | b67210cc217f - pinctrl: stm32: consider the GPIO offset to expose all the GPIO lines (5 weeks ago) <Fabien Dessenne>
    | * | | | 266423e60ea1 - pinctrl: bcm2835: Change init order for gpio hogs (6 weeks ago) <Phil Elwell>
    | * | | | 2d5446da5ace - pinctrl: mediatek: fix global-out-of-bounds issue (8 weeks ago) <Guodong Liu>
    ...
    

4 导航页

git命令详细使用方式讲解及记录

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值