蓝狮注册登陆在 Git 中如何配置用户信息

大家好,我是前端西瓜哥。

最近买了台新的笔记本,重新装了一些软件,这次就说说怎么在 git 中配置用户信息吧。

当我们安装了 git 后,一件非常重要的事情就是配置我们的用户名和邮箱地址,因为我们提交代码到远端服务器需要通过它们来得知提交者是谁。

查看配置列表
在配置用户信息前,我们需要确定自己是否已配置了用户信息。

我们先查看所有的配置:

git config –list
如果在一个 git 仓库下输入这个命令,蓝狮注册登陆你会得到类似下面的内容:

credential.helper=osxkeychain
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
core.precomposeunicode=true
remote.origin.url=git@github.com:F-star/svg-editor.git
remote.origin.fetch=+refs/heads/:refs/remotes/origin/
branch.main.remote=origin
branch.main.merge=refs/heads/main
配置分为全局配置(global)和本地配置(local)。全局配置影响所有的 git 仓库,本地配置只影响它所在的 git 仓库,并可以覆盖全局的配置。

上面的内容中,除了第一行来自全局配置,其他配置都是来自该 git 仓库,蓝狮注册开户具体配置文件位置在 .git/config

[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote “origin”]
url = git@github.com:F-star/svg-editor.git
fetch = +refs/heads/:refs/remotes/origin/
[branch “main”]
remote = origin
merge = refs/heads/main
全局配置来自当前用户家目录下的 .gitconfig 文件,即 ~/.gitconfig 。

用编辑器(通常是 vim)打开配置文件的命令如下:

打开全局配置

git config –global –edit

打开当前 git 仓库配置

git config –edit
(希望你至少知道该如何退出 vim,祝福)。

查看指定配置
上面列表内容有点多,我们可以只看需要的用户信息配置。

查看配置的用户的 用户名/邮箱地址:

git config user.name
git config user.email
命令会先找 git 仓库里的配置,找不到再找全局配置。如果什么都没输出,说明你没有配置。

你也可以指定配置的作用域为 local 还是 global:

本地

git config –local user.email

全局

git config –global user.email
如果都没有,就要去配置了。

配置用户信息
配置全局的用户信息:

git config –global user.name “前端西瓜哥”
git config –global user.email “work-email@gmail.com”
双引号可加可不加,如果值中间有空格符,就要加上。

如果你想配置当前项目的用户信息,将 –global 去掉即可,或者也可以改成 –local。

git config user.email “person-email@gmail.com”
这在你用公司的电脑折腾自己的个人项目很有用,毕竟你也不希望自己的个人项目的 commit 提交显示的是公司邮箱。

删除配置
当不需要一个配置时,我们可以使用 –unset 配置项。也可以直接改配置文件。

本地

git config –unset user.email

全局

git config –global –unset user.email
结尾
你学会了吗?

0 Comments
Leave a Reply