Git实战笔记(一) 常见错误

这篇文章将会记录一些在使用 Git 过程中经常会遇到的错误及其解决办法(持续更新)


错误 1

> git push
# fatal: unable to access 'https://github.com/xxx/xxx.git/': Failed to connect to github.com port 443: Timed out

【解决办法】

这种情况可能是网络波动引起的,可以尝试更换网络环境或者多试几次


错误 2

> git push
# fatal: repository 'https://github.com/xxx/xxx.git/' not found

【解决办法】

这种情况可能是权限验证错误引起的,可以尝试卸载 Git 凭证管理器后重装

> git credential-manager uninstall # 卸载

> git credential-manager install   # 安装

错误 3

> git push
# fatal: unable to access 'https://github.com/xxx/xxx.git/': OpenSSL SSL_read: Connection was reset, errno 10054

【解决办法】

这种情况可能是 SSL 验证失败导致的,我们可以直接禁止 SSL 验证

git config --global http.sslVerify "false"

官网上关于 http.sslVerify 的说明如下:

Whether to verify the SSL certificate when fetching or pushing over HTTPS. Defaults to true. Can be overridden by the GIT_SSL_NO_VERIFY environment variable.

通过 HTTPS 抓取和推送时是否进行 SSL 验证。默认为 true。可以通过环境变量 GIT_SSL_NO_VERIFY 进行重写。


错误 4

> git push
# Enumerating objects: 76, done.
# Counting objects: 100% (76/76), done.
# Delta compression using up to 4 threads
# Compressing objects: 100% (59/59), done.
# Writing objects: 100% (60/60), 808.06 MiB | 10.78 MiB/s, done.
# Total 60 (delta 32), reused 0 (delta 0), pack-reused 0
# error: RPC failed; curl 18 transfer closed with outstanding read data remaining
# send-pack: unexpected disconnect while reading sideband packet
# fatal: the remote end hung up unexpectedly
# Everything up-to-date

【解决办法】

  • 这种情况有可能是缓存过小引起的,我们可以尝试增大缓存(缓存大小根据实际情况设置)
git config --global http.postBuffer 1048576000

# 单位为 Byte,如 1048576000B 就是 1G

http.postBuffer 实际上做了什么?Git 官网上有一段相关的说明:

Maximum size in bytes of the buffer used by smart HTTP transports when POSTing data to the remote system. For requests larger than this buffer size, HTTP/1.1 and Transfer-Encoding: chunked is used to avoid creating a massive pack file locally. Default is 1 MiB, which is sufficient for most requests.

使用 smart HTTP 协议向远程 post 数据时,缓存区的最大大小,单位为 byte。对大于这个缓存大小的请求,会使用 HTTP/1.1 和 Transfer-Encoding: chunked 避免在本地创建一个庞大的打包文件。默认为 1 MiB,对于大多数请求而言已经足够。

Note that raising this limit is only effective for disabling chunked transfer encoding and therefore should be used only where the remote server or a proxy only supports HTTP/1.0 or is noncompliant with the HTTP standard. Raising this is not, in general, an effective solution for most push problems, but can increase memory consumption significantly since the entire buffer is allocated even for small pushes.

注意,提高这个限制只对禁用分块传输的编码有效,因此只应在远程服务或代理只支持 HTTP/1.0 或不符合 HTTP 标准的情况下使用。一般来说,提高这个限制对大多数推送问题不是一个有效的解决方案,但会大大增加内存消耗,因为即使对于小规模的推送也会分配整个缓冲区。

  • 这种情况也可能是网络波动导致的,我们可以尝试取消相关的网络限制
git config --global http.lowSpeedLimit 0
git config --global http.lowSpeedTime 999999

http.lowSpeedLimithttp.lowSpeedTime 实际上做了什么?官网上有一段相关的说明:

If the HTTP transfer speed is less than http.lowSpeedLimit for longer than http.lowSpeedTime seconds, the transfer is aborted. Can be overridden by the GIT_HTTP_LOW_SPEED_LIMIT and GIT_HTTP_LOW_SPEED_TIME environment variables.

若 HTTP 传输速度小于 http.lowSpeedLimit 或传输时间大于 http.lowSpeedTime,那么该传输就会中断。可以通过环境变量 GIT_HTTP_LOW_SPEED_LIMITGIT_HTTP_LOW_SPEED_TIME 进行重写。

  • 如果传输的文件实在太大,可以试试增大压缩率(压缩率大小根据实际情况设置)
git config --global core.compression 3

core.compression 实际上做了什么?官网上有一段相关的说明:

An integer -1…9, indicating a default compression level. -1 is the zlib default. 0 means no compression, and 1…9 are various speed/size tradeoffs, 9 being slowest. If set, this provides a default to other compression variables, such as core.looseCompression and pack.compression.

用一个整数 -1 … 9 表示压缩级别。-1 表示使用 zlib 默认值。0 意味着不压缩,1 … 9 是压缩速度/压缩大小的权衡,9 压缩速度最慢/压缩大小最小。如果这个值被设置,那么将会给其它压缩相关的变量提供一个默认值,例如 core.looseCompression 和 pack.compression。

  • 如果有多次提交一起推送,可以试试分多次推送【重要】
git push <远程仓库名称> <commit id>:<远程分支名称>

# 这个命令会推送指定 <commit id> 前所有未推送的 commit
  • 10
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Git是一个非常流行的版本控制系统,但有时候在使用中会出现错误。下面是一些常见Git错误代码以及解决办法: 1. 错误代码:fatal: refusing to merge unrelated histories 解决办法:在执行git pull命令时添加--allow-unrelated-histories参数,即git pull origin master --allow-unrelated-histories 2. 错误代码:error: failed to push some refs to 'git@github.com:XXX/XXX.git' 解决办法:先执行git pull命令更新本地代码,再执行git push命令推送代码 3. 错误代码:fatal: not a git repository (or any of the parent directories): .git 解决办法:进入正确的git仓库目录 4. 错误代码:fatal: unable to access 'https://github.com/XXX/XXX.git/': OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 解决办法:检查网络连接,或者将https改为ssh协议,即git@github.com:XXX/XXX.git 5. 错误代码:error: Your local changes to the following files would be overwritten by merge 解决办法:先执行git stash命令,将本地修改保存起来,再执行git pull命令更新代码,最后执行git stash pop命令恢复本地修改 6. 错误代码:fatal: the remote end hung up unexpectedly 解决办法:检查网络连接,或者将git缓存设置为最大值,即git config --global http.postBuffer 524288000 7. 错误代码:error: pathspec 'XXX' did not match any file(s) known to git 解决办法:检查输入的文件名是否正确,或者执行git add命令添加文件 这些是常见Git错误代码和解决办法,希望能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值