彻底解决 git push 的【pack exceeds maximum allowed size】

查看 57|回复 1
作者:飞龙使者   
其实一个 Git 远程仓库除了限制单文件大小和仓库大小之外,还有一个单次推送的大小。比如 Github 它的单次推送大小大约是 5GB,如果超出就会报这个错误。
一些历史久远的仓库,虽然单文件大小和仓库大小没有超出限制,但是如果你一次性pull再push,就会超出这个限制。
解决办法很简单,就是按照提交的顺序一个一个 push。为此我专门写了个 Python 脚本,来实现自动化。
核心代码:
# 逐个推送提交
def git_push_per_commit(args):
    dir = args.dir
    work_branch = args.branch
    remote = args.remote
    print(f'branch: {work_branch}, remote: {remote}')
    if not is_git_repo(dir):
        print('请提供 GIT 本地仓库')
        return
    # 检查分支是否存在
    branches = get_all_branches(dir)
    if work_branch not in branches:
        print(f'分支 {work_branch} 不存在')
        return
    # 如果远程仓库名为地址,创建别名
    if remote.startswith('https://') or \
        remote.startswith('git@'):
            url, remote = remote, uuid.uuid4().hex
            subp.Popen(
                ['git', 'remote', 'add', remote, url],
                shell=True, cwd=dir,
            ).communicate()
    # 检查远程库是否存在
    remotes = get_remote_names(dir)
    if remote not in remotes:
        print(f'远程仓库 {remote} 不存在')
        return
    # 检查远程库是否有该分支
    subp.Popen(
        ['git', 'remote', 'update', remote],
        shell=True, cwd=dir,
    ).communicate()
    branches = get_all_branches(dir)
    remote_exi =  f'remotes/{remote}/{work_branch}' in branches
    if not remote_exi:
        # 如果远程分支不存在,推送本地分支所有提交
        cids = get_branch_cids(dir, work_branch)
    else:
        # 拉取远程分支,并重命名
        remote_branch = f'{remote}-{work_branch}-{uuid.uuid4().hex}'
        subp.Popen(
            ['git', 'fetch', remote, f'{work_branch}:{remote_branch}'],
            shell=True, cwd=dir,
        ).communicate()
        # 查看远程库是否有新提交
        cids = get_branch_cids(dir, remote_branch, '^' + work_branch)
        if cids:
            print('远程仓库有新的提交,需要手动 git pull')
            print('\n'.join(cids))
            return
        # 查看本地库的新提交
        cids = get_branch_cids(dir, work_branch, '^' + remote_branch)
    for cid in cids[::-1]:
        cid_branch = 'cid-' + cid
        cmds = [
            # 切换分支
            ['git', 'checkout', cid, '-f'],
            ['git', 'branch', cid_branch],
            # 提交改动
            ['git', 'push', remote, f'{cid_branch}:{work_branch}'],
        ]
        for cmd in cmds:
            subp.Popen(cmd, shell=True, cwd=dir).communicate()
args是数据对象,打包所有所需参数,包括dir——本地仓库路径,remote远程仓库别名或者地址,branch要推送的本地分支名称(假定远程分支名称和本地一样,不一样的你本地重命名一下就好了)。
代码做必要的检查之后,直接获取提交 ID 然后按时间顺序 push。如果远程仓库已经 push 了一些东西,那就把 ID 做个差。
其它依赖函数都在 apachecn/BookerPubTool 里面,就不贴出来了。各位也可以封装 GitPython 来实现。
一键调用:
pip install BookerPubTool
pub-tool git-push  [-b
] [-r ]

仓库, 分支

sgbyg   

.....................................
您需要登录后才可以回帖 登录 | 立即注册

返回顶部