git config 命令使用

Git 是分布式版本控制系统,所以,每个机器都必须自报家门:user.name 和 user.email
请添加图片描述

如果不配置用户信息,是无法进行后续操作的。

全局配置

git config --global user.name "Your username"
git config --global user.email "Your email"

可以用下面的命令为 Git 启用额外的颜色

git config --global color.ui true

局部配置

需要在当前仓库下进行

git config user.name "Your username"
git config user.email "Your email"

git 级别优先级

  1. 仓库级 - local 【最高】
  2. 全局级 - global 【中等】
  3. 系统级 - system 【最低】

全局级配置文件 - global

  1. Linux 的 .gitconfig 文件一般在当前用户主目录 cd ~
    请添加图片描述

  2. Windows 的 .gitconfig 文件路径一般是 C:\Users\Administrator

通过命令查看全局级配置文件:git config --global -l

请添加图片描述

仓库级配置文件 - local

Git 仓库级配置文件都在当前仓库下的 .git/config 里面
请添加图片描述

Windows 中的 .git 文件是隐藏的,需要在文件管理器中勾选【隐藏的项目】
请添加图片描述

通过命令查看仓库级配置文件:git config --local -l

请添加图片描述

系统级配置文件 - system

Git 系统级配置文件都 Git 安装目录下的 /etc/gitconfig 里面

请添加图片描述

通过命令查看系统级配置文件:git config --system -l

请添加图片描述

查看配置文件

命令参数:–list

简写:-l

格式:git config [–local | --global | --system] -l

  • 仓库级 config:git config --local -l
  • 全局级 config:git config --global -l
  • 系统级 config:git config --system -l
  • 当前生效的配置:git config -l

编辑配置文件

命令参数:–edit

简写:-e

格式:git config [–local | --global | --system] -e

  • 仓库级 config:git config --local -e
  • 全局级 config:git config --global -e
  • 系统级 config:git config --system -e
  • 默认编辑仓库级 config:git config -e

使用 :q 来退出编辑器

注:执行这个命令的时候,Git 会用配置文件中设定的编辑器打开配置文件

增加配置项

命令参数:–add

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

  • 默认是添加在 local 配置文件,且 section,key,value 一项都不能少

例如我们执行命令:git config --global --add idol.name Liyuu 就能够给全局级配置文件中添加配置项
请添加图片描述

获取配置项

命令参数:–get

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

  • 默认是获取 local 配置文件里的配置项

  • 直接使用 git config section.key 省略 --get 也可以
    请添加图片描述

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

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

删除配置项

命令参数:–unset

格式:git config [–local | --global | --system] --unset section.key
请添加图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个命令是用来设置 Git 的身份验证信息的。如果你想要在 Git使用 GitHub 的 API,或者是在 GitHub 上进行代码的推送和拉取,那么你需要先在 GitHub 上生成一个 Personal Access Token,然后使用这个命令将这个 Token 添加到 Git 的配置文件中。 具体的命令格式如下: ``` git config --global credential.helper store git config --global user.name "Your Name" git config --global user.email "[email protected]" ``` 其中,`credential.helper` 用来设置 Git 的凭证管理器,这里我们使用 `store`,表示将凭证信息存储在本地。`user.name` 和 `user.email` 分别用来设置 Git 的用户名和邮箱地址。 然后,你需要在 GitHub 上生成一个 Personal Access Token,并将这个 Token 添加到 Git 的配置文件中。具体的步骤如下: 1. 在 GitHub 上登录你的账号,并进入 Settings 页面。 2. 在左侧菜单栏中选择 Developer settings -> Personal access tokens。 3. 点击 Generate new token 按钮,输入一个描述信息,并勾选需要的权限。 4. 点击 Generate token 按钮,生成一个新的 Personal Access Token。 5. 将这个 Token 复制到剪贴板中。 6. 在终端中输入以下命令,将 Token 添加到 Git 的配置文件中: ``` git config --global credential.helper store git config --global user.name "Your Name" git config --global user.email "[email protected]" git config --global credential.helper 'cache --timeout=3600' git config --global credential.helper '!f() { echo "password=$1"; }; f' ``` 7. 接着,使用以下命令将 Token 添加到 Git 的凭证管理器中: ``` git credential approve protocol=https host=github.com username=<your_username> password=<your_personal_access_token> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值