git通過ssh方式連接gitlab(解決early EOF)

背景

​ 最近在使用git clone,pull或push代碼時經常出現一些異常,導致不能更新和提交代碼,比如以下異常:

$ git clone http://192.168.1.207:9999/mes_projects_group/applicationdesigner.git
Cloning into ‘applicationdesigner’…
remote: Enumerating objects: 16, done.
remote: Counting objects: 100% (16/16), done.
remote: Compressing objects: 100% (15/15), done.
error: RPC failed; curl 56 Malformed encoding found in chunked-encoding
fatal: the remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed

$ git clone http://192.168.1.207:9999/mes_projects_group/applicationdesigner.git
Cloning into ‘applicationdesigner’…
remote: Enumerating objects: 16, done.
remote: Counting objects: 100% (16/16), done.
remote: Compressing objects: 100% (15/15), done.
error: inflate: data stream error (invalid literal/lengths set)
fatal: pack has bad object at offset 934679: inflate returned -3,-5
fatal: index-pack failed

$ git clone http://192.168.1.207:9999/mes_projects_group/applicationdesigner.git
Cloning into ‘applicationdesigner’…
remote: Enumerating objects: 16, done.
remote: Counting objects: 100% (16/16), done.
remote: Compressing objects: 100% (15/15), done.
fetch-pack: protocol error: bad band #104
fatal: early EOF
fatal: index-pack failed

網上提供的解決方案大多爲修改或添加配置,如

git config –global http.postBuffer 524288000
git config –global core.compression -1
git config –global core.compression 0

或直接修改當前用戶目錄下的.gitconfig文件

[core]
packedGitLimit = 512m
packedGitWindowSize = 512m
[pack]
deltaCacheSize = 2047m
packSizeLimit = 2047m
windowMemory = 2047m

或是clone時加 –depth 1 等。

這裏有大部分的解決方法[https://stackoverflow.com/questions/15240815/git-fatal-the-remote-end-hung-up-unexpectedly]

​ 但是都不能解決問題;最後看到了這個文[https://blog.csdn.net/fastjack/article/details/79757520]的第一句話,然後我就抱着試一試的心態,嘗試了一下,竟然成功了,下面就介紹這個方法。

ssh介紹

​ SSH 爲 [Secure Shell](https://baike.baidu.com/item/Secure Shell) 的縮寫,由 IETF 的網絡小組(Network Working Group)所制定;SSH 爲建立在應用層基礎上的安全協議。SSH 是較可靠,專爲遠程登錄會話和其他網絡服務提供安全性的協議。利用 SSH 協議可以有效防止遠程管理過程中的信息泄露問題。SSH最初是UNIX系統上的一個程序,後來又迅速擴展到其他操作平臺。SSH在正確使用時可彌補網絡中的漏洞。SSH客戶端適用於多種平臺。gitlab的ssh詳細介紹參考https://docs.gitlab.com/ee/ssh/

生成sshkey

在git bash或命令行輸入

ssh-keygen -t rsa -C "[email protected]"

[ssh-keygen環境變量設置path=C:\Program Files\Git\usr\bin]
連續回車即可,結果如下

Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/NP/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/NP/.ssh/id_rsa
Your public key has been saved in /c/Users/NP/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:djdVhsLZ0yarmsp07zlHzpQeVO/GiLj2t89QeYfq79U [email protected]
The key’s randomart image is:
+—[RSA 3072]—-+

+—-[SHA256]—–+

如果已存在,會提示,根據需要選則是否重新生成

/c/Users/NP/.ssh/id_rsa already exists.
Overwrite (y/n)?

生成的ssh相關密鑰在用戶目錄的.ssh文件夾下

id_rsa.pub(公鑰),id_rsa(私鑰)

多個sshkey並存參考[https://www.cnblogs.com/fanyong/p/3962455.html]

登記sshkey

1.用記事本打開id_rsa.pub(公鑰),複製裏面的內容

2.登錄自己的gitlab,進入用戶設置界面,點擊SSH Keys界面。

 

如圖,粘貼後點擊【add key】即可。

3.測試ssh連接,輸入命令行

ssh -T [email protected]
出現 Welcome to GitLab, @WangLeLe!表示成功

整個過程如下!

使用ssh從gitlab服務器clone代碼

git clone [email protected]:mes_projects_group/newpwrplatform.cloud.git
Cloning into ‘newpwrplatform.cloud’…
remote: Enumerating objects: 382, done.
remote: Counting objects: 100% (382/382), done.
remote: Compressing objects: 100% (190/190), done.
remote: Total 15627 (delta 229), reused 342 (delta 189)
Receiving objects: 100% (15627/15627), 8.67 MiB | 12.03 MiB/s, done.
Resolving deltas: 100% (11858/11858), done.

獲取成功。只有地址和http方式不同命令全部一樣。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章