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

在Ubuntu Docker镜像中通过Tox运行GitPython测试时Git提交失败

GitPython在GitHub Actions中提交代码提示Author identity unknown的解决办法

问题背景

在GitHub Actions的Ubuntu Docker镜像中,通过Tox运行使用GitPython操作本地Git仓库的单元测试。镜像已配置好包含user.name和user.email的全局.gitconfig,且能通过GitPython的GitConfigParser确认配置无误,但执行commit代码时仍报错:

git.exc.GitCommandError: Cmd('git') failed due to: exit code(128)
  cmdline: git commit -m Initial commit
  stderr: '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: unable to auto-detect email address (got '**********.(none)')'

原因分析

  • Tox默认会隔离测试环境,可能导致全局git配置无法被当前测试进程读取
  • GitPython调用底层git命令时,可能没有继承到包含全局配置的用户上下文或环境变量

解决方案

1. 直接在目标仓库中设置用户身份

跳过全局配置依赖,给要提交的仓库单独设置user.name和user.email,确保commit时能读取到身份信息:

# 在执行commit前添加仓库级配置
config_writer = remote_repo.config_writer()
config_writer.set_value("user", "name", "测试用户").release()
config_writer.set_value("user", "email", "test@example.com").release()

# 原有提交代码
with open(os.path.join(remote_repo.working_dir, 'readme.md'), 'w') as file:
    file.write('# Remote\n')
remote_repo.git.add(A=True)
remote_repo.git.commit(m=commit_text)

2. 从全局配置同步到仓库

如果不想硬编码身份信息,可以读取全局.gitconfig的内容,同步到当前操作的仓库:

from git import GitConfigParser
import os

# 读取全局git配置
global_config_path = os.path.expanduser("~/.gitconfig")
global_config = GitConfigParser(global_config_path)
user_name = global_config.get_value("user", "name")
user_email = global_config.get_value("user", "email")

# 应用到当前仓库
config_writer = remote_repo.config_writer()
config_writer.set_value("user", "name", user_name).release()
config_writer.set_value("user", "email", user_email).release()

# 执行提交
with open(os.path.join(remote_repo.working_dir, 'readme.md'), 'w') as file:
    file.write('# Remote\n')
remote_repo.git.add(A=True)
remote_repo.git.commit(m=commit_text)

3. 在GitHub Actions工作流中显式设置全局身份

在workflow步骤中重新设置git全局身份,确保当前执行上下文能读取到正确配置:

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Set git global identity
        run: |
          git config --global user.name "GitHub Actions"
          git config --global user.email "actions@github.com"
      
      - name: Run tests with Tox
        run: tox

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 11:46:05