一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

git config怎么用?常用配置命令及添加删除配置项的做法实用指南

时间:2026-09-30 08:52:01 编辑:袖梨 来源:一聚教程网

平时做技术实践时,很多问题不是概念不会,而是细节没串起来。拿“git config怎么用?常用配置命令及添加删除配置项的做法”来说,它看着像小点,放到项目里常会牵出环境、配置、兼容性和维护成本。下面按实际采用顺序,把思路、关键写法和容易踩坑的地方讲清楚,便于大家直接对照操作。

一、config文件分类

Git中有三层config文件:系统、全局、本地

查看不同级别的设置文件:

#查看系统config
git config --system --list
  
#查看当前用户(global)配置
git config --global --list
 
#查看当前仓库配置信息
git config --local --list
  • 实际处理时,/etc/gitconfig:包含了适用来系统所有用户和所有项目的值。注是git的安装目录(Win:D:Gitmingw64etcgitconfig) --system 系统级
  • 结合项目来看,~/.gitconfig:只适用来当前登录用户的设置。(Win:C:UsersAdministrator.gitconfig) --global 全局

从实现思路看,位于git项目目录中的.git/config:适用来特定git项目的设置。–local当前项目

注意:对于同一设置项,三个设置文件的优先级是1<2<3

落到代码里,Git 采用一系列设置文件来保存你自定义的行为。 它首先会查找系统级的 /etc/gitconfig 文件,该文件含有系统里每位用户及他们所拥有的仓库的设置值。

结合项目来看,若你传递 --system 选项给 git config,它就会读写该文件。

落到代码里,下面 Git 会查找每个用户的 ~/.gitconfig 文件(或者 ~/.config/git/config 文件)。 你能够传递 --global 选项让 Git 读写该文件。

从实现思路看,最后 Git 会查找你正在操作的仓库所对应的 Git 目录下的设置文件(.git/config)。 这个文件中的值只对该仓库有效,它对应于向 git config 传递 --local 选项。

从实现思路看,以上三个层次中每层的设置(系统、全局、本地)都会覆盖掉上一层次的设置,所以 .git/config 中的值会覆盖掉 /etc/gitconfig 中所对应的值。

Note:

从实现思路看,Git 的设置文件是纯文本的,所以你能够直接手动编辑这些设置文件,输入合乎语法的值。 但是运行 git config 命令会更轻松些。

二、设置用户名与邮箱(用户标识,必要)

1、当你安装Git后首先要做的事情是设置你的用户名称和e-mail地址。

从实现思路看,这是很重要的,因为每次Git提交都会采用该信息。它被永远的嵌入到了你的提交中:

$ git config --global user.name "***"  #名称
$ git config --global user.email ****@qq.com #邮箱

实际处理时,只需做一次这个设置,如果你传递了–global 选项,因为Git将总是会采用该信息来处理你在系统中所做的一切操作。

在这个场景下,若你希望在一个特定的项目中采用不同的名称或e-mail地址,你能够在该项目中运行该命令而不要–global选项。 总之–global为全局设置,不加为某个项目的特定设置。

修改如上图2所示;

2、添加或删除设置项

1)、添加设置项

git config [--local|--global|--system]  section.key value

[--local|--global|--system] #可选的,对应本地,全局,系统不同级别的设置
section.key #区域下的键
value #对应的值
--local 项目级
--global 当前用户级
--system 系统级

2)、删除设置项

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

3、更多设置项

git config --global color.ui true   #打开所有的默认终端着色
git config --global alias.ci commit #别名 ci 是commit的别名
[alias]
co = checkout
ci = commit
st = status
pl = pull
ps = push
dt = difftool
l = log --stat
cp = cherry-pick
ca = commit -a
b = branch

user.name #用户名
user.email #邮箱
core.editor #文本编辑器
merge.tool #差异分析工具
core.paper "less -N" #配置显示方式
color.diff true #diff颜色配置
alias.co checkout #设置别名
git config user.name #获得用户名
git config core.filemode false #忽略修改权限的文件

4、所有config命令参数

语法: git config [<options>]        
        
文件位置
    --global #use global config file 使用全局配置文件
    --system #use system config file 使用系统配置文件
    --local #use repository config file 使用存储库配置文件
    -f, --file <file> #use given config file 使用给定的配置文件
    --blob <blob-id> #read config from given blob object 从给定的对象中读取配置
        
动作
    --get #get value: name [value-regex] 获得值:[值]名[正则表达式]
    --get-all #get all values: key [value-regex] 获得所有值:[值]名[正则表达式]
    --get-regexp #get values for regexp: name-regex [value-regex] 得到的值根据正则
    --get-urlmatch #get value specific for the URL: section[.var] URL 为URL获取特定的值
    --replace-all #replace all matching variables: name value [value_regex] 替换所有匹配的变量:名称值[ value_regex ]
    --add #add a new variable: name value 添加一个新变量:name值
    --unset #remove a variable: name [value-regex] 删除一个变量名[值]:正则表达式
    --unset-all #remove 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 #open an editor 打开一个编辑器
    --get-color #find the color configured: [default] 找到配置的颜色:插槽[默认]
    --get-colorbool #find the color setting: [stdout-is-tty] 发现颜色设置:槽[ stdout是TTY ]
        
类型
    --bool #value is "true" or "false" 值是“真”或“假”。
    --int #value is decimal number 值是十进制数。
    --bool-or-int #value is --bool or --int 值--布尔或int
    --path #value is a path (file or directory name) 值是路径(文件或目录名)
        
其它
    -z, --null #terminate values with NUL byte 终止值与null字节
    --name-only #show variable names only 只显示变量名
    --includes #respect include directives on lookup 尊重包括查找指令
    --show-origin #show origin of config (file, standard input, blob, command line) 显示配置(文件、标准输入、数据块、命令行)的来源

三、解决git每次提交和拉取代码需输入用户名和密码问题

输入以下命令行:

git config --global credential.helper store

再输入一次正确的用户名和密码,就能够了

总结

落到代码里,总的来说,git config适合结合实际项目边做边理解。先抓住核心思路,再逐步补上细节和边界处理,最后效果会更稳定,也更容易复用。

热门栏目