git基础教程(5) 掌握git的基本配置gitconfig

本文详细介绍了git配置的三个级别(本地、全局、系统)及其对应配置文件的位置,讲解了如何查看和编辑这些配置,并阐述了配置生效的优先级规则。此外,还涵盖了添加、获取和删除配置项的操作,以及在实际使用中配置用户名和邮箱的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

提示:本文经奇妙之二进制二次编辑和审核, 修正了一些错误,包括但不限于错别字,语句不通顺,代码编译不过,命令书写错误,图片缺失,排版优化等。

在git中,我们使用git config 命令用来配置git的配置文件,git配置级别主要有以下3类:

1、仓库级别 local 【优先级最高】

2、用户级别 global【优先级次之】

3、系统级别 system【优先级最低】

通常:

git 仓库级别对应的配置文件是当前仓库下的.git/config 【在当前目录下.git目录默认是隐藏的,所以在文件管理器中我们要打开显示以藏文件】

img

git 用户级别对应的配置文件是用户宿主目录下的.gitconfig 【宿主目录:C:\Users\xiong】

img

git系统级别对应的配置文件是git安装目录下的 /etc/gitconfig

当然我们可以在cmd命令提示符中输入以下查看配置信息。

1、git config --local -l 查看仓库级别配置【必须要进入到具体的目录下,比如要查看TestGit仓库的配置信息】

2、git config --global -l 查看用户配置

3、git config --system -l 查看系统配置

img

4、git config -l查看所有的配置信息,依次是系统级别、用户级别、仓库级别

img

我们可以使用下面命令全面的查看git的配置信息:

小静静@DESKTOP-MD21325 MINGW64 /d/xx/test (master)
$ git config -l  --show-origin  --show-scope
system  file:D:/software/Git/etc/gitconfig      diff.astextplain.textconv=astextplain
system  file:D:/software/Git/etc/gitconfig      filter.lfs.clean=git-lfs clean -- %f
system  file:D:/software/Git/etc/gitconfig      filter.lfs.smudge=git-lfs smudge -- %f
system  file:D:/software/Git/etc/gitconfig      filter.lfs.process=git-lfs filter-process
system  file:D:/software/Git/etc/gitconfig      filter.lfs.required=true
system  file:D:/software/Git/etc/gitconfig      http.sslbackend=openssl
system  file:D:/software/Git/etc/gitconfig      http.sslcainfo=D:/software/Git/mingw64/ssl/certs/ca-bundle.crt
system  file:D:/software/Git/etc/gitconfig      core.autocrlf=true
system  file:D:/software/Git/etc/gitconfig      core.fscache=true
system  file:D:/software/Git/etc/gitconfig      core.symlinks=false
system  file:D:/software/Git/etc/gitconfig      pull.rebase=false
system  file:D:/software/Git/etc/gitconfig      credential.helper=manager-core
system  file:D:/software/Git/etc/gitconfig      credential.https://dev.azure.com.usehttppath=true
global  file:"C:/Users/\345\260\217\351\235\231\351\235\231/.gitconfig" user.name=hjh
global  file:"C:/Users/\345\260\217\351\235\231\351\235\231/.gitconfig" user.email=hjhvictory@163.com
local   file:.git/config        core.repositoryformatversion=0
local   file:.git/config        core.filemode=false
local   file:.git/config        core.bare=false
local   file:.git/config        core.logallrefupdates=true
local   file:.git/config        core.symlinks=false
local   file:.git/config        core.ignorecase=true
local   file:.git/config        remote.origin.url=git@gitee.com:hongjiaheng/test.git
local   file:.git/config        remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
local   file:.git/config        branch.master.remote=origin
local   file:.git/config        branch.master.merge=refs/heads/master

第一列显示变量的作用范围(级别),第二列显示变量来自哪个级别的配置文件,第三列就是变量值对。

5.git config 常用配置选项
  • git config -e 编辑仓库级别配置文件

  • git config --local -e 编辑仓库级别配置文件

  • git config --global -e 编辑用户级别配置文件

  • git config --system -e 编辑系统级别配置文件

    如果在安装git时选择了vim作为编辑器,那么会弹出以下界面:
    在这里插入图片描述

  • git config 添加配置项目

  • git config --global user.email “you@example.com”

  • git config --global user.name “Your Name”

上面的操作表示添加用户级别的配置信息,也就是说修改用户宿主目录下面的配置文件

6. 配置文件如何生效的

对于git来说,配置文件的权重是仓库>全局>系统。Git会使用这一系列的配置文件来存储你定义的偏好,它首先会查找/etc/gitconfig文件(系统级),该文件含有对系统上所有用户及他们所拥有的仓库都生效的配置值。接下来Git会查找每个用户的~/.gitconfig文件(全局级)。最后Git会查找由用户定义的各个库中Git目录下的配置文件.git/config(仓库级),该文件中的值只对当前所属仓库有效。

7.增加配置项

参数 --add

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

注意add后面的section,key,value一项都不能少,否则添加失败。比如我们执行:git config –add cat.name tom

img

8.获取一个配置项

有时候,我们并不需要查看所有配置的值,而是查看某个配置项的值,怎么做呢?

命令参数 --get

格式:git config [–local|–global|–system] --get section.key(默认是获取local配置中内容)

我们先往global配置中写入一个cat.name=Tomcat的配置项,再使用git config --get cat.name看看得到的是什么:

img

结果就是local中的cat.name=Tom,因此git config --get section.key 等价于git config --local --get section.key

如果获取一个section不存在的key值,不会返回任何值

如果获取一个不存在的section的key值,则会报错

9.删除一个配置项

命令参数 –unset

格式:git config [–local|–global|–system] --unset section.key

相信有了前两个命令的使用基础,大家举一反三就知道改怎么用了,来,我们试试删除local配置中的cat.name

img
总结
例子:一个变量别人名字是liwenlong,我想改为自己的名字:

//查
git config --global --list
 
git config --global user.name
 
//增
git config  --global --add user.name jianan
 
//删
git config  --global --unset user.name
 
//改
git config --global user.name jianan

必要配置

对于一个新的仓库,我们可以什么都不配置,但是一定要配置用户名和邮箱地址:

配置个人的用户名称和电子邮件地址:

$ git config --global user.name "runoob"
$ git config --global user.email test@runoob.com

如果用了 –global 选项,那么更改的配置文件就是位于你用户主目录下的那个,以后你所有的项目都会默认使用这里配置的用户信息。

如果要在某个特定的项目中使用其他名字或者电邮,只要去掉 --global 选项重新配置即可,新的设定保存在当前项目的 .git/config 文件里。

如果我们没有配置,在提交代码时会有如下错误:

xxx@DESKTOP-MD21325 MINGW64 /d/test/test (master)
$ git commit -m "feature: add readme"
Author identity unknown

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'xxx@DESKTOP-MD21325.(none)')


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值