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

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

其实一个 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 <dir> [-b <branch=master>] [-r <remote=origin>]
相关文章
|
14天前
|
开发工具 git
git 使用之remote: File [4e21e71a555febaa4dfaaa05cf7eeb606ea96ae2] size 104.090MB, exceeds quota 100MB remote: Please remove the file[s] from history and try again 报错如何解决-优雅草卓伊凡
git 使用之remote: File [4e21e71a555febaa4dfaaa05cf7eeb606ea96ae2] size 104.090MB, exceeds quota 100MB remote: Please remove the file[s] from history and try again 报错如何解决-优雅草卓伊凡
38 3
git 使用之remote: File [4e21e71a555febaa4dfaaa05cf7eeb606ea96ae2] size 104.090MB, exceeds quota 100MB remote: Please remove the file[s] from history and try again 报错如何解决-优雅草卓伊凡
|
15天前
|
安全 开发工具 git
git分布式版本控制系统及在码云上创建项目并pull和push
通过本文的介绍,我们详细讲解了Git的基本概念和工作流程,并展示了如何在码云上创建项目及进行pull和push操作。Git作为一种分布式版本控制系统,为开发者提供了强大的工具来管理代码变更和协作开发。希望本文能帮助您更好地理解和使用Git及码云,提高开发效率和代码质量。
44 18
|
14天前
|
安全 开发工具 git
git分布式版本控制系统及在码云上创建项目并pull和push
通过本文的介绍,我们详细讲解了Git的基本概念和工作流程,并展示了如何在码云上创建项目及进行pull和push操作。Git作为一种分布式版本控制系统,为开发者提供了强大的工具来管理代码变更和协作开发。希望本文能帮助您更好地理解和使用Git及码云,提高开发效率和代码质量。
34 16
|
3月前
|
网络安全 开发工具 git
解决fatal:remote error:You can’t push to git://github.com/username/*.g
通过上述诊断与修复步骤,绝大多数的推送错误都能得到有效解决,确保您的Git工作流顺畅无阻。
311 1
|
5月前
|
开发工具 git
GIT:如何合并已commit的信息并进行push操作
通过上述步骤,您可以有效地合并已提交的信息,并保持项目的提交历史整洁。记得在执行这些操作之前备份当前工作状态,以防万一。这样的做法不仅有助于项目维护,也能提升团队协作的效率。
225 4
|
5月前
|
开发工具 git
GIT:如何合并已commit的信息并进行push操作
通过上述步骤,您可以有效地合并已提交的信息,并保持项目的提交历史整洁。记得在执行这些操作之前备份当前工作状态,以防万一。这样的做法不仅有助于项目维护,也能提升团队协作的效率。
320 3
|
6月前
|
开发工具 git 开发者
|
8月前
|
Shell 网络安全 开发工具
git实现服务器自动push拉取代码--webhooks
git实现服务器自动push拉取代码--webhooks
652 1
|
8月前
|
网络安全 开发工具 数据安全/隐私保护
git pull/push每次都需要输入密码问题
git pull/push每次都需要输入密码问题
498 0
|
8月前
|
Linux 开发工具 git
解决 Linux git push 贡献者不同(没有出现绿点)的问题
解决 Linux git push 贡献者不同(没有出现绿点)的问题