51Testing软件测试论坛

标题: 配置用户信息---get [打印本页]

作者: 梦幻小丑灯    时间: 2022-9-20 13:38
标题: 配置用户信息---get
最近买了台新的笔记本,重新装了一些软件,这次就说说怎么在 git 中配置用户信息吧。
  当我们安装了 git 后,一件非常重要的事情就是配置我们的用户名和邮箱地址,因为我们提交代码到远端服务器需要通过它们来得知提交者是谁。
  查看配置列表
  在配置用户信息前,我们需要确定自己是否已配置了用户信息。
  我们先查看所有的配置:
  1.   git config --list
复制代码
如果在一个 git 仓库下输入这个命令,你会得到类似下面的内容:

  1. credential.helper=osxkeychain
  2.   core.repositoryformatversion=0
  3.   core.filemode=true
  4.   core.bare=false
  5.   core.logallrefupdates=true
  6.   core.ignorecase=true
  7.   core.precomposeunicode=true
  8.   remote.origin.url=git@github.com:F-star/svg-editor.git
  9.   remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
  10.   branch.main.remote=origin
  11.   branch.main.merge=refs/heads/main
复制代码
配置分为全局配置(global)和本地配置(local)。全局配置影响所有的 git 仓库,本地配置只影响它所在的 git 仓库,并可以覆盖全局的配置。
  上面的内容中,除了第一行来自全局配置,其他配置都是来自该 git 仓库,具体配置文件位置在 .git/config
  1. [core]
  2.           repositoryformatversion = 0
  3.           filemode = true
  4.           bare = false
  5.           logallrefupdates = true
  6.           ignorecase = true
  7.           precomposeunicode = true
  8.   [remote "origin"]
  9.           url = git@github.com:F-star/svg-editor.git
  10.           fetch = +refs/heads/*:refs/remotes/origin/*
  11.   [branch "main"]
  12.           remote = origin
  13.           merge = refs/heads/main
复制代码
全局配置来自当前用户家目录下的 .gitconfig 文件,即 ??~/.gitconfig??。
  用编辑器(通常是 vim)打开配置文件的命令如下:
  1.  # 打开全局配置
  2.   git config --global --edit
  3.   # 打开当前 git 仓库配置
  4.   git config --edit
复制代码
(希望你至少知道该如何退出 vim,祝福)。
  查看指定配置
  上面列表内容有点多,我们可以只看需要的用户信息配置。
  查看配置的用户的 用户名/邮箱地址:
  1.  git config user.name
  2.   git config user.email
复制代码
命令会先找 git 仓库里的配置,找不到再找全局配置。如果什么都没输出,说明你没有配置。
  你也可以指定配置的作用域为 local 还是 global:
  1.  # 本地
  2.   git config --local user.email
  3.   # 全局
  4.   git config --global user.email
复制代码
如果都没有,就要去配置了。
  配置用户信息
  配置全局的用户信息:
  1. git config --global user.name "前端西瓜哥"
  2.   git config --global user.email "work-email@gmail.com"
复制代码
 双引号可加可不加,如果值中间有空格符,就要加上。
  如果你想配置当前项目的用户信息,将 --global 去掉即可,或者也可以改成 --local。
  1.  git config user.email "person-email@gmail.com"
复制代码
这在你用公司的电脑折腾自己的个人项目很有用,毕竟你也不希望自己的个人项目的 commit 提交显示的是公司邮箱。
  删除配置
  当不需要一个配置时,我们可以使用 --unset 配置项。也可以直接改配置文件。
  1.  # 本地
  2.   git config --unset user.email
  3.   # 全局
  4.   git config --global --unset user.email
复制代码
 结尾
  你学会了吗?














欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2