如何知道配置时保存的 git 用户名和邮箱?

How to know the git username and email saved during configuration?

配置时 git 我 运行 这两个命令:

git config --global user.name "My Name"

git config --global user.email "myemail@example.com"

但是,我怀疑我是否打错了字。那么,是否有任何命令可以知道 git 在配置期间保存的 nameemail?显然,我可以通过查看提交历史知道使用 git log 命令。但为此我必须做出承诺,对吧?我可以在命令行的帮助下知道吗?

命令git config --list 将列出设置。在那里你还应该找到 user.nameuser.email.

考虑到@Robert 所说的,我尝试使用 config 命令,似乎有一种直接的方法可以知道姓名和电子邮件。

要知道用户名,请输入:

git config user.name

要知道电子邮件,请键入:

git config user.email

这两个分别只输出姓名和电子邮件,不需要查看整个列表。派上用场。

在您的 git 存储库目录中、运行 git config user.name.

Why is running this command within your git repo directory important?

如果您在 git 存储库之外,git config user.name 会在 全局 级别为您提供 user.name 的值。进行提交时,将在 local 级别读取关联的用户名。

虽然不太可能,假设 user.name 全局 级别定义为 foo,但在 本地级别定义为 bar 级。然后,当您 运行 git config user.name 在 git 存储库目录之外时,它会给出 bar。但是,当您真正提交某些内容时,关联的值为 foo.


Git config variables can be stored in 3 different levels. Each level overrides values in the previous level.

1.系统级别(适用于系统上的每个用户及其所有存储库)

  • 查看,git config --list --system(可能需要sudo
  • 设置,git config --system color.ui true
  • 编辑系统配置文件,git config --edit --system

2。全局级别(您个人的特定值,用户。)

  • 查看,git config --list --global
  • 设置,git config --global user.name xyz
  • 编辑全局配置文件,git config --edit --global

3。存储库级别(特定于该单个存储库)

  • 查看,git config --list --local
  • 设置,git config --local core.ignorecase true--local可选)
  • 编辑存储库配置文件,git config --edit --local--local 可选)

How to view all settings?

  • 运行 git config --list,显示 systemglobal,以及(如果在存储库中)本地 配置
  • 运行 git config --list --show-origin, 也显示每个配置项的源文件

How to read one particular config?

    例如
  • 运行git config user.name得到user.name
  • 您还可以指定选项 --system--global--local 以在特定级别读取该值。

参考:1.6 Getting Started - First-Time Git Setup

If you want to check or set the user name and email you can use the below command

检查用户名

git config user.name

设置用户名

git config user.name "your_name"

检查您的电子邮件

git config user.email

Set/change 你的邮箱

git config user.email "your@email.com"

List/see全部配置

git config --list

在这里加上我的两分钱。如果您只想获得 user.name 或 user.email 而没有详细输出。

在 liunx 上,键入 git config --list | grep user.name

在 windows 上,输入 git config --list | findstr user.name

这只会给你 user.name。

有时上述解决方案在 macbook 中无法获取用户名和密码。

IDK 为什么?,这里我有另一个解决方案。

$ git credential-osxkeychain get
host=github.com
protocol=https

this will revert username and password

查看git配置类型-

git config --list

要全局更改用户名,请输入 -

git config --global user.name "your_name"

要全局更改电子邮件类型 -

git config --global user.email "your_email"

列出配置文件中设置的所有变量及其值。

git config --list

如果您是 git 的新手,请使用以下命令设置用户名和电子邮件地址。

设置用户名

git config --global user.name "your Name"

设置用户邮箱

git config --global user.email "your_email@example.com"

检查用户名

git config user.name

检查用户邮箱

git config user.email

所有好的讨论都在这里。这里只想补充一点:在很多tool/app中,都有一个user id的概念,用来唯一标识一个用户。用户 ID 通常是一个单词(没有嵌入 space),或者在某些情况下是一个电子邮件地址。所以我带着这个问题来到这个话题: git 是否有另一个名为用户 ID 的字段?如果不是,git 使用什么作为识别用户的唯一方式?

到目前为止,从阅读这里的答案和通过网络搜索,我在 git 中找不到任何提及用户 ID。 一种。所以我不得不得出结论,没有user id这样的字段。 b.用户名不太可能是一个好的候选者,因为人们可以有相同的名字,而且名字是自由形式的。 C。那么剩下的唯一合乎逻辑的选择就是邮箱地址,或者邮箱地址+用户名?

确实,可以从 say xgboost 源代码的 git 日志中看到两者:

commit 5c2d7a18c92b445ba897d4b0b4859508ae720883 (HEAD -> master, origin/master, origin/HEAD)
Author: Jiaming Yuan <jm.yuan@outlook.com>
Date:   Tue Jun 15 14:08:26 2021 +0800

    Parallel model dump for trees. (#7040)

设置全局/所有项目

首先获取并确认旧值:

git config --global user.email
git config --global user.name

输出:(如果git新安装那么输出将显示为空)

yourold@email.com
youroldgoodname

现在设置新值

git config --global user.email yournew@email.com
git config --global user.name yournewgoodname

对于当前工作区或项目
只需从上面的所有内容中删除 --global commands/lines

首先检查如果您在 git 配置中已有姓名和电子邮件:-

检查,

  • 用户名:-

    git 配置 --global user.name

  • 邮箱

    git 配置 --global user.email

如果已经设置,你想更改或者如果没有设置,你想在 git 配置中添加用户名和电子邮件:-

设置:-

  • 用户名:-

    git 配置 --global user.name ""

  • 邮箱:-

    git 配置 --global user.email ""