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

如何用GitPython撤销提交?Python脚本执行revert时卡顿求助

解决GitPython调用revert时卡顿无响应的问题

我来帮你搞定这个卡顿的问题!你遇到的情况大概率是git revert默认触发了交互模式导致的——在Windows环境下,Git会自动打开编辑器让你编辑revert的提交信息,但脚本运行在非交互环境里,没有输入来源,进程就一直卡着等输入。结合你的Python2.7+Windows7环境,给你两个靠谱的解决方案:

1. 给revert命令加--no-edit参数跳过交互

这是最直接的修复方式,告诉Git不要打开编辑器,直接用默认提交信息完成revert。修改你的代码如下:

from git import Repo

repo = Repo("你的仓库本地路径")
target_branch = "需要处理的分支名"

def is_commit_in_branch(repo, commit, branch_name):
    # 用git branch --contains检查提交是否存在于目标分支
    check_result = repo.git.branch("--contains", commit.hexsha, branch_name)
    return len(check_result.strip()) > 0

# 遍历master分支的提交
for commit in repo.iter_commits("master"):
    if is_commit_in_branch(repo, commit, target_branch):
        # 执行revert,添加--no-edit跳过交互步骤
        try:
            repo.git.revert(commit.hexsha, "--no-edit")
            print(f"成功撤销提交: {commit.hexsha}")
        except Exception as e:
            print(f"撤销提交失败: {str(e)}")
            # 遇到冲突时执行merge重置
            repo.git.reset("--merge")
    else:
        # 提交不存在,执行cherry-pick添加
        try:
            repo.git.cherry_pick(commit.hexsha)
            print(f"成功添加提交: {commit.hexsha}")
        except Exception as e:
            print(f"添加提交失败: {str(e)}")
            repo.git.cherry_pick("--abort")

2. 改用subprocess直接调用Git命令(Python2.7更稳定)

考虑到Python2.7的GitPython版本可能存在兼容性问题,直接调用原生Git命令有时会更可靠。Python2.7没有subprocess.run,我们用check_output和call来实现:

import subprocess
import os

repo_path = "你的仓库本地路径"
target_branch = "需要处理的分支名"

def is_commit_in_branch(commit_hash, branch_name):
    os.chdir(repo_path)
    try:
        # 检查提交是否在目标分支上
        output = subprocess.check_output(
            ["git", "branch", "--contains", commit_hash, branch_name],
            stderr=subprocess.STDOUT
        )
        return len(output.strip()) > 0
    except subprocess.CalledProcessError:
        return False

# 先切换到目标分支
os.chdir(repo_path)
subprocess.call(["git", "checkout", target_branch])

# 获取master分支的所有提交哈希
log_output = subprocess.check_output(["git", "log", "--pretty=format:%H", "master"])
commit_hashes = log_output.strip().split("\n")

for commit_hash in commit_hashes:
    if is_commit_in_branch(commit_hash, target_branch):
        # 执行revert操作
        try:
            subprocess.check_output(
                ["git", "revert", commit_hash, "--no-edit"],
                stderr=subprocess.STDOUT
            )
            print(f"成功撤销提交: {commit_hash}")
        except subprocess.CalledProcessError as e:
            print(f"撤销提交失败: {e.output}")
            subprocess.call(["git", "reset", "--merge"])
    else:
        # 执行cherry-pick操作
        try:
            subprocess.check_output(
                ["git", "cherry-pick", commit_hash],
                stderr=subprocess.STDOUT
            )
            print(f"成功添加提交: {commit_hash}")
        except subprocess.CalledProcessError as e:
            print(f"添加提交失败: {e.output}")
            subprocess.call(["git", "cherry-pick", "--abort"])

卡顿原因说明

Git的revert命令默认会启动交互式编辑器(比如Windows自带的记事本)来让你修改revert的提交信息。脚本运行在非交互环境中,没有用户输入,Git进程就会一直处于等待状态,表现为脚本卡顿无响应。加上--no-edit参数后,Git会直接使用默认提交信息,跳过交互步骤,自然就不会阻塞了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:22:56