Git config 配置

一、config文件分类

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

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

#查看系统config
git config --system --list
  
#查看当前用户(global)配置
git config --global  --list
 
#查看当前仓库配置信息
git config --local  --list
  1. /etc/gitconfig:包含了适用于系统所有用户和所有项目的值。注是git的安装目录(Win:D:\Git\mingw64\etc\gitconfig) --system 系统级

  2. ~/.gitconfig:只适用于当前登录用户的配置。(Win:C:\Users\Administrator.gitconfig) --global 全局

  3. 位于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所示;

  1. 添加或删除配置项
    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
  1. 更多配置项
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  #忽略修改权限的文件  
  1. 所有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: slot [default]    找到配置的颜色:插槽[默认]
    --get-colorbool           #find the color setting: slot [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

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

  • 21
    点赞
  • 104
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Git config是用来配置Git的命令行工具的命令。它允许你设置和修改Git的各种配置选项。根据引用中的内容,可以通过以下方式来配置Git的用户信息: 1. 查看全局配置信息:使用命令`git config --global --list`可以查看当前全局配置的所有信息。 2. 增加用户名称:使用命令`git config --global --add user.name your_name`可以将新的用户名添加到全局配置中。 3. 删除用户名称:使用命令`git config --global --unset user.name`可以删除全局配置中的用户名。 4. 修改用户名称:使用命令`git config --global user.name your_name`可以修改全局配置中的用户名。 此外,根据引用的内容,还可以通过以下方式查看不同级别的配置文件: 1. 查看系统级配置信息:使用命令`git config --system --list`可以查看系统级的配置文件(/etc/gitconfig)中的所有信息。 2. 查看全局配置信息:使用命令`git config --global --list`可以查看当前用户(global)配置文件中的所有信息。 3. 查看当前仓库配置信息:使用命令`git config --local --list`可以查看当前仓库配置文件中的所有信息。 综上所述,Git config命令可以用来配置Git的各种选项,包括用户名称等信息,并且可以通过不同的命令和选项来查看不同级别的配置文件。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [git基础教程(5) 掌握git的基本配置gitconfig](https://blog.csdn.net/HandsomeHong/article/details/120805196)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Git config 配置](https://blog.csdn.net/u010274449/article/details/124179992)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值