Skip to content

解决代码提交失败

一、解决类似报错

text
git -c core.quotepath=false -c log.showSignature=false push --progress --porcelain origin refs/heads/master:master
Enumerating objects: 17, done.
Counting objects:   5% (1/17)
Counting objects:  11% (2/17)
...
Writing objects:  68% (11/16)
error: RPC failed; HTTP 500 curl 22 The requested URL returned error: 500
send-pack: unexpected disconnect while reading sideband packet
Writing objects:  75% (12/16)

Writing objects: 100% (16/16)
Writing objects: 100% (16/16), 1.73 MiB | 4.47 MiB/s, done.
Total 16 (delta 0), reused 0 (delta 0), pack-reused 0
fatal: the remote end hung up unexpectedly

原因

text
缓冲区大小默认为 1MB,当传一些比如图片时会报此错。所以要确保服务器的 Git 配置允许足够大的数据传输。

解决方法:增加本地缓冲区大小

shell
# 设置大小为500M
git config --global http.postBuffer 524288000

二、解决类似报错

text
The push operation includes a file which exceeds GitHub's file size restriction of 100MB. Please remove the file from history and try again.
Files that exceed the limit

原因

text
项目中存在超过100MB的大文件

解决方法:使用 .gitattributes 配置 Git LFS 自动管理大文件

shell
# 安装 Git LFS
git lfs install

# 在项目根目录下创建或编辑 .gitattributes 文件,指定哪些文件类型应该用 LFS 管理
touch .gitattributes

# 规则语法
pattern filter=lfs diff=lfs merge=lfs -text

# Demo
# 跟踪所有 ZIP 文件
*.zip filter=lfs diff=lfs merge=lfs -text

# 跟踪所有视频文件
*.mp4 filter=lfs diff=lfs merge=lfs -text
*.mov filter=lfs diff=lfs merge=lfs -text

# 跟踪所有 Photoshop 文件
*.psd filter=lfs diff=lfs merge=lfs -text

# 跟踪 assets 目录下所有文件
assets/** filter=lfs diff=lfs merge=lfs -text

# 跟踪特定大文件
/path/to/large/file.data filter=lfs diff=lfs merge=lfs -text

# A:提交并推送更改
git add .gitattributes
git commit -m "配置 Git LFS 跟踪大文件"
git push origin main

# B:如果仓库中已有大文件,需要明确跟踪它们
# 跟踪特定类型
git lfs track "*.psd"
# 或者跟踪特定文件
git lfs track "path/to/large/file.data"
# 然后重新添加这些文件
git add path/to/large/file.data
git commit -m "将大文件迁移到 LFS"
git push origin main

# 检查哪些文件被 LFS 跟踪
git lfs ls-files