前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >彻底解决 git push 的【pack exceeds maximum allowed size】

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

作者头像
ApacheCN_飞龙
发布2023-03-08 21:19:19
1.2K0
发布2023-03-08 21:19:19
举报
文章被收录于专栏:信数据得永生信数据得永生

其实一个 Git 远程仓库除了限制单文件大小和仓库大小之外,还有一个单次推送的大小。比如 Github 它的单次推送大小大约是 5GB,如果超出就会报这个错误。

一些历史久远的仓库,虽然单文件大小和仓库大小没有超出限制,但是如果你一次性pull再push,就会超出这个限制。

解决办法很简单,就是按照提交的顺序一个一个 push。为此我专门写了个 Python 脚本,来实现自动化。

核心代码:

代码语言:javascript
复制
# 逐个推送提交
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 来实现。

一键调用:

代码语言:javascript
复制
pip install BookerPubTool
pub-tool <dir> [-b <branch=master>] [-r <remote=origin>]
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-02-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档