> For the complete documentation index, see [llms.txt](https://pshizhsysu.gitbook.io/git/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pshizhsysu.gitbook.io/git/guan-yu-config/guan-yu-name-yu-email.md).

# 关于name与email

我们一般在主机上装好git后，都需要通过以下的两条命令设置user.name与user.email信息

```
$ git config --global user.name xxxx
$ git config --global user.email xxxx@example.com
```

当我们有多台主机向git服务器提交代码时， 此时git服务器就需要知道每一次提交是谁的。此时，就是通过user.name来进行区分。

比如，我们从两台不同的主机上向gogs中的learngit项目进行两次提交，在该项目中就可以看到两次提交的user.name是不一样的（注意，尽管两次提交用的都是gogs中的pshizh同一个用户）

![](/files/-M69E3k1hH-jR-wG5TJ2)

上图中的`docker162`就是162主机上的user.name，`docker163`就是163主机上的user.name

但是但是，在github上，commit history中显示的不是主机上设置的user.name，而是github的账号（当我们git push到github时，是携带了github账号的信息的）：

![](/files/-M69E3k2Ey0_4LkgRP7W)

## 设置用户名与邮箱

```
$ git config --[local|global|system] user.name xxx
$ git config --[local|global|system] user.email xxx@example.com
```

在设置用户名与邮箱的时候，可以设置不同的范围：

* local：当前仓库，在当前仓库的`.git/config`文件中
* global：当前用户，在当前用户的`~/.gitconfig`文件中
* system：系统范围，在`/etc/gitconfig`文件中

**最佳实践：有可能同一台主机需要连接多个远程仓库，而且每个远程仓库的用户名可能不一样，所以最好使用`--local`选项给每个项目单独设置，设置为远程仓库的登录用户名。**

## 查看用户名与邮箱

```
$ git config user.name
$ git config user.email
```

上面的命令查找的范围依次是local、global、system。

## Reference

\[1] <https://blog.csdn.net/hutaoer06051/article/details/8275069>

\[2] <https://www.jianshu.com/p/d24e791a7679>
