彻底解决 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>]
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)