通过分支删除进行Git强制推送 | AI生成和翻译

Home 2025.09

#!/usr/bin/env python3
"""
git_force_push.py

该脚本通过先删除远程分支,再推送本地分支的方式实现"强制推送"。
这在仓库允许分支删除但不允许强制推送的情况下有效。

用法: python git_force_push.py <remote> <branch>
示例: python git_force_push.py origin main

注意:此操作假设您拥有足够的权限且允许分支删除。
请谨慎使用,因为删除分支可能会丢失提交记录。
"""

import subprocess
import sys

def run_command(command):
    """运行shell命令并返回其输出。"""
    try:
        result = subprocess.run(command, shell=True, capture_output=True, text=True, check=True)
        return result.stdout.strip()
    except subprocess.CalledProcessError as e:
        print(f"运行命令 '{command}' 时出错: {e}")
        print(f"标准错误: {e.stderr}")
        sys.exit(1)

if __name__ == "__main__":
    if len(sys.argv) != 3:
        print("用法: python git_force_push.py <remote> <branch>")
        sys.exit(1)

    remote = sys.argv[1]
    branch = sys.argv[2]

    # 删除远程分支
    delete_cmd = f"git push {remote} :{branch}"
    print(f"正在删除远程分支: {delete_cmd}")
    run_command(delete_cmd)

    # 推送本地分支
    push_cmd = f"git push {remote} {branch}"
    print(f"正在推送本地分支: {push_cmd}")
    run_command(push_cmd)

    print("强制推送已成功完成。")

工作原理


Back

x-ai/grok-code-fast-1

Donate