You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Git提交报错未读取全局.gitconfig配置的用户身份信息

运行环境
  • 主机:AWS EC2实例
  • 操作系统:Ubuntu 22.04
  • Git版本:2.34.1
  • 当前普通用户ubuntu的全局Git配置文件~/.gitconfig内容如下:
[credential "https://github.com"]
        helper =
        helper = !/usr/bin/gh auth git-credential
[credential "https://gist.github.com"]
        helper =
        helper = !/usr/bin/gh auth git-credential
[safe]
        directory = /var/www/html/wix
        directory = /var/www/html/wix-native-app
[user]
        name = JXX XXXha
        email = jXXXX0@gmail.com
        signingkey = 8XXXXXXXXXXX1
[commit]
        gpgsign = true
问题表现

在/var/www/html/wix-native-app目录下执行sudo git commit时抛出如下错误,提示未配置用户身份:

ubuntu@ip-172-31-46-106:/var/www/html/wix-native-app$ sudo git commit
Author identity unknown

*** Please tell me who you are.

Run

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

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: no email was given and auto-detection is disabled

已经在当前用户的全局.gitconfig中配置了用户名、邮箱等身份信息,仍无法正常完成提交。

故障原因

执行sudo命令时,Git是以root用户身份运行的,默认会读取root家目录下的全局配置文件/root/.gitconfig,不会加载当前ubuntu用户家目录下的配置,因此找不到预先配置的用户身份参数。

修复方案

根据实际使用场景选一种即可:

  • 方案一(优先推荐):调整项目目录权限,去掉不必要的sudo提权
    绝大多数场景下执行git命令不需要root权限,只需要把项目目录的所有者改成当前ubuntu用户,后续直接用普通用户身份执行git操作即可,不会有权限问题,也能避免后续GPG签名、凭据读取的权限异常:
    sudo chown -R ubuntu:ubuntu /var/www/html/wix-native-app
    # 后续直接提交即可,不需要加sudo
    git commit
    
  • 方案二:给root用户同步配置Git参数
    如果确实需要用sudo执行git命令,直接给root用户配置对应的全局Git参数即可:
    sudo git config --global user.name "JXX XXXha"
    sudo git config --global user.email "jXXXX0@gmail.com"
    sudo git config --global user.signingkey 8XXXXXXXXXXX1
    sudo git config --global commit.gpgsign true
    
    如果需要复用gh的GitHub认证能力,可以直接把当前用户的.gitconfig复制到root家目录并修正权限:
    sudo cp /home/ubuntu/.gitconfig /root/
    sudo chown root:root /root/.gitconfig
    

    注意:如果开启了GPG签名提交,还需要确保root用户能访问到对应的GPG私钥,否则提交时会出现签名失败报错。

  • 方案三:单次执行时指定加载当前用户的Git配置
    如果只是临时需要用sudo执行一次提交,可以在命令中指定加载当前用户的git配置文件:
    sudo git -c include.path=/home/ubuntu/.gitconfig commit
    

内容的提问来源于stack exchange,提问作者thisisjaymehta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.01 03:09:44