---
url: https://eoht.net/fe/git/user.md
description: >-
  说明 Git 的 user.name、user.email 如何写入提交记录，区分全局与仓库配置，并演示配置来源检查、SSH 密钥生成和 GitHub
  连接验证，避免把提交身份当成登录凭据。
---

# Git 用户名与邮箱配置：提交身份、配置范围和 SSH 认证 {#configure-git-user-identity}

Git 提交中的作者姓名和邮箱由 `user.name`、`user.email` 决定；它们不负责登录 GitHub，也不会赋予仓库推送权限。先配置提交身份，再按远程地址选择 HTTPS 或 SSH 认证。

## 查看 Git 配置信息 {#view-git-configuration}

```sh
git config --list --show-origin
```

`--show-origin` 会显示每项配置来自哪个文件。个人全局配置通常对当前用户的仓库生效，仓库内的本地配置可以覆盖它。完整配置可能含私有地址或认证相关信息，排查身份问题时优先查看下面两项。

## 查看用户名、邮箱与配置来源 {#view-git-credentials-and-email}

在目标仓库目录中运行：

```sh
git config --show-origin --get user.name
git config --show-origin --get user.email
```

没有输出通常表示尚未设置。`user.password` 不是 Git 标准的提交身份或登录密码设置；HTTPS 凭据应由平台支持的认证方式和凭据管理器处理。

## 设置用户名和邮箱 {#set-git-username-and-email}

将示例替换为希望出现在提交记录中的姓名和邮箱，保留普通英文引号：

```sh
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
```

姓名可以是公开署名，不必与 GitHub 登录用户名相同。若希望 GitHub 将提交关联到账号，使用该账号已验证的邮箱，或账号设置中提供的 `noreply` 邮箱。

工作项目需要不同身份时，在该仓库内设置本地值：

```sh
git config --local user.name "Work Name"
git config --local user.email "work@example.com"
```

设置后重新执行上一节的查询，确认生效值和来源。修改身份影响之后创建的提交，不会自动重写已有提交。配置范围可参考 [Git 官方入门文档](https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup)。

## 使用 SSH 认证 {#use-git-over-ssh}

先检查是否已有可用 SSH 密钥；已有密钥无需为了配置 Git 姓名而重新生成。确需创建时，在终端或 Windows Git Bash 中运行：

```sh
ssh-keygen -t ed25519 -C "you@example.com"
```

按提示选择保存位置和密钥口令。若提示文件已存在，使用新的文件名，不要覆盖仍在使用的密钥。采用默认名称时：

| 文件 | 用途 |
| --- | --- |
| `~/.ssh/id_ed25519.pub` | 公钥，添加到 GitHub 的 SSH keys 设置 |
| `~/.ssh/id_ed25519` | 私钥，保留在本机，不上传到仓库或粘贴到网站 |

按 [GitHub 官方 SSH 指南](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent) 加载密钥并添加公钥后，验证连接：

```sh
ssh -T git@github.com
git remote -v
```

首次连接先核对 [GitHub 公布的主机指纹](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/githubs-ssh-key-fingerprints)。认证成功的提示应对应预期账号；GitHub 不提供交互式 Shell。远程地址为 `git@github.com:组织或用户名/仓库.git` 时才使用 SSH，`https://` 地址仍使用 HTTPS 认证。

## Git 学习路径 {#git-learning-path}

如果尚未完成安装，请先查看 [Git 简介与安装](/fe/git/install)；配置好提交身份后，继续学习 [Git 常用命令](/fe/git/command)。
