方法一命令行

在项目根目录打开 cmd 或者 powershell

# 设置当前项目(本地)提交名
git config --local user.name "你的名字"

# 设置当前项目(本地)邮箱
git config --local user.email "你的邮箱@公司.com"

验证是否生效

# 查看当前项目配置(带 --local 只看本地)
git config --local user.name
git config --local user.email

# 对比全局配置
git config --global user.name

关键区别

表格

范围

命令

作用域

优先级

全局

git config --global

当前系统用户所有仓库

系统

git config --system

整台机器所有用户

最低

本地

git config --local

仅当前项目

最高

Git 提交时优先使用 本地(local) 配置,覆盖全局。

方法二:直接改配置文件

在项目根目录下:

# 编辑 .git/config
vim .git/config

添加用户配置:

[user]
    name = 你的名字
    email = 你的邮箱@公司.com

快速检查当前项目用的哪个用户

git config --show-origin user.name
git config --show-origin user.email
file:.git/config  张三          ← 本地配置生效
file:C:/Users/xxx/.gitconfig  李四   ← 全局配置被覆盖