使用Repo.git.commit()创建空提交时的参数错误问题
解决GitPython提交空提交的参数错误问题
问题分析
你当前的代码存在两个关键问题导致参数报错:
- 直接调用
Repo.git.commit而非实例化本地仓库的Repo对象后调用对应方法 - 未正确传递
--allow-empty命令行参数,且保留了不需要的author参数
修正后的代码
from git import Repo import os from dotenv import load_dotenv load_dotenv() full_local_path = os.getenv('full_local_path') username = os.getenv('username') password = os.getenv('password') remote = f"https://{username}:{password}@github.com/myRepo/myRepo.git" # 克隆仓库(本地路径已存在时会报错,可提前判断路径是否存在做兼容) Repo.clone_from('git@github.com:myRepo/myRepo.git', full_local_path) # 实例化本地仓库对象 repo = Repo(full_local_path) # 执行空提交:传入--allow-empty参数,移除author参数 repo.git.commit('--allow-empty', '-m', 'empty commit')
关键说明
- 必须先通过本地仓库路径创建
Repo实例,才能调用对应的git.commit方法操作目标仓库 --allow-empty是git commit的原生命令行选项,直接作为位置参数传入即可启用空提交功能- 移除
author参数后,提交会使用本地Git配置的默认作者信息
内容的提问来源于stack exchange,提问作者c0d3rbox
相关产品推荐
相关产品推荐

