一、基本概述
git目前支持4種的通訊協議:本地協議(Local)、ssh、http(Dumb啞協議和Smart智能協議)和Git協議
1.1、本地協議介紹
本地協議是基於本地文件系統或者共享的文件系統進行訪問,也就是說遠程版本庫就是硬盤中另外的一個目錄。
因此搭建起來就特別容易,可以完成代碼的版本管理。適合臨時搭建使用。
以下代碼做演示
首先我在e盤創建一個共享文件夾git_repo,然后創建一個裸倉庫做交互
1 ## 2 hjjie@mrforever MINGW64 /e 3 $ cd git_repo/ 4 5 hjjie@mrforever MINGW64 /e/git_repo 6 $ git init --bare share.git 7 Initialized empty Git repository in E:/git_repo/share.git/
然后我在另外一個文件夾中clone空項目下來,並放入一個文本文件,然后git add -》git commit -》git push,提交代碼到共享文件夾
1 hjjie@mrforever MINGW64 /e/git_study 2 $ git clone /e/git_repo/share.git/ 3 Cloning into 'share'... 4 warning: You appear to have cloned an empty repository. 5 done. 6 7 hjjie@mrforever MINGW64 /e/git_study 8 $ cd share/ 9 10 hjjie@mrforever MINGW64 /e/git_study/share (master) 11 $ echo 'hello git' > git.txt; git add -A; git commit -am 'first commit'; git push; 12 warning: LF will be replaced by CRLF in git.txt. 13 The file will have its original line endings in your working directory. 14 [master (root-commit) cea04b4] first commit 15 1 file changed, 1 insertion(+) 16 create mode 100644 git.txt 17 Counting objects: 3, done. 18 Writing objects: 100% (3/3), 207 bytes | 0 bytes/s, done. 19 Total 3 (delta 0), reused 0 (delta 0) 20 To E:/git_repo/share.git/ 21 * [new branch] master -> master
當然,除了直接訪問外,還可以通過file協議進行訪問 git clone file:///e/git_repo/share.git/
1 hjjie@mrforever MINGW64 /e/git_study2 2 $ git clone file:///e/git_repo/share.git/ 3 Cloning into 'share'... 4 remote: Counting objects: 3, done. 5 remote: Total 3 (delta 0), reused 0 (delta 0) 6 Receiving objects: 100% (3/3), done.
這樣子,會將相關資源打包再傳輸過來,在傳輸過來的倉庫中會發現打包后的文件
1.2、SSH協議(Secure SHell)
git本身支持ssh協議,而且利用ssh協議搭建git服務器也很簡單,ssh協議訪問也是很高效安全,
只是在linux系統上直接使用這樣裸的Git服務,本身權限方面就比較不靈活,因為需要依賴Linux自身的訪問權限
在Linux服務器上安裝Git可以看這里:
以下是演示的過程:
首先在Linux上初始化一個裸倉庫
1 [root@service103 apps]# mkdir git-repo 2 [root@service103 apps]# ls 3 git git-repo jdk1.8 pack 4 [root@service103 apps]# cd git-repo/ 5 [root@service103 git-repo]# git init --bare remote.git 6 Initialized empty Git repository in /apps/git-repo/remote.git/ 7 [root@service103 git-repo]#
然后在本地嘗試clone git clone root@192.1.1.103:/apps/git-repo/remote.git 並加入文本然后git add -》 git commit -》 git push
1 hjjie@mrforever MINGW64 /e/git_study 2 $ git clone root@192.1.1.103:/apps/git-repo/remote.git 3 Cloning into 'remote'... 4 The authenticity of host '192.1.1.103 (192.1.1.103)' can't be established. 5 ECDSA key fingerprint is SHA256:SNb2t6iAVaQuHd1FvOdB2lPSVASbU07i8raTpllD0aE. 6 Are you sure you want to continue connecting (yes/no)? yes 7 Warning: Permanently added '192.1.1.103' (ECDSA) to the list of known hosts. 8 root@192.1.1.103's password: 9 warning: You appear to have cloned an empty repository. 10 11 hjjie@mrforever MINGW64 /e/git_study 12 $ ls 13 remote/ share/ 14 15 hjjie@mrforever MINGW64 /e/git_study 16 $ cd remote/ 17 18 hjjie@mrforever MINGW64 /e/git_study/remote (master) 19 $ echo 'hello git' > git.txt; 20 21 hjjie@mrforever MINGW64 /e/git_study/remote (master) 22 $ git add -A; git commit -am 'first ssh commit'; git push; 23 warning: LF will be replaced by CRLF in git.txt. 24 The file will have its original line endings in your working directory. 25 [master (root-commit) 4f831ab] first ssh commit 26 1 file changed, 1 insertion(+) 27 create mode 100644 git.txt 28 root@192.1.1.103's password: 29 Counting objects: 3, done. 30 Writing objects: 100% (3/3), 210 bytes | 0 bytes/s, done. 31 Total 3 (delta 0), reused 0 (delta 0) 32 To 192.1.1.103:/apps/git-repo/remote.git 33 * [new branch] master -> master
1.3、Http協議(Dump\Smart)
Git的http協議通訊是依靠WEB容器來實現的,而在Git1.6.6版本以前就只提供啞協議,可以理解為是只讀的,
不能做代碼的推送。而在啞協議中,它會基於web容器將版本庫的內容當作靜態文件訪問讀取的,
所以搭建起來比較簡單,在這里使用nginx。還有在這里演示啞協議
首先在linux服務器上
## 初始化一個空倉庫 git init --bare remote.git ## 開啟post-upadte鈎子 cd remote.git/hooks mv post-update.sample post-update ./post-update
然后配置nginx.conf,並啟動nginx
http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root /apps/git-repo; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
然后在本地嘗試克隆到創建文本到最后的推送,clone的地址 git clone http://192.1.1.103/remote.git remote
可以看到能夠拉取代碼,但是不能推送代碼。
hjjie@mrforever MINGW64 /e/git_study3 $ git clone http://192.1.1.103/remote.git remote Cloning into 'remote'... warning: You appear to have cloned an empty repository. hjjie@mrforever MINGW64 /e/git_study3 $ cd remote/ hjjie@mrforever MINGW64 /e/git_study3/remote (master) $ echo 'hello git' > git.txt; git add -A; git commit -am 'first commit'; git pus h; warning: LF will be replaced by CRLF in git.txt. The file will have its original line endings in your working directory. warning: LF will be replaced by CRLF in git.txt. The file will have its original line endings in your working directory. [master (root-commit) fcebf87] first commit 1 file changed, 1 insertion(+) create mode 100644 git.txt error: Cannot access URL http://192.1.1.103/remote.git/, return code 22 fatal: git-http-push failed error: failed to push some refs to 'http://192.1.1.103/remote.git'
另外查看nginx的access.log文件可以看出更多的訪問細節,是通過一系列的引用Refs,最后指引到需要的靜態文件
1.4、Git協議
Git協議是Git 里的一個特殊的守護進程然后會監聽在一個特定的端口(9418),類似於 SSH 服務。
要讓版本庫支持 Git 協議,需要先創建一個 git-daemon-export-ok
文件.是 Git 協議守護進程為這個版本庫提供服務的必要條件。
另外它的訪問速度很快的,但缺乏授權機制,還有一般企業公司不會開放9418這個非標准的端口。
在Linux服務器上:
cd apps/git-repo/remote.git ## 創建一個空文件 touch git-daemon-export-ok ## 以守護進程方式啟動git服務 $nohub git daemon --reuseaddr --base-path=/apps/git-repo/ /apps/git-repo/ &
然后在本地嘗試克隆 git clone git://192.1.1.103:9418/remote.git
hjjie@mrforever MINGW64 /e/git_study4 $ git clone git://192.1.1.103:9418/remote.git Cloning into 'remote'... warning: You appear to have cloned an empty repository.
1.5、最后
當然,在實際的工作環境上也很少基於不同協議直接操作裸倉庫。很多都是通過Git的Web可視化服務來使用。
如GitLab,Gogs等等,提供更多的功能與權限控制。只是了解Git的通訊協議對Git的底層有更好的理解。
本文作者:hjjay
原文出處:https://www.cnblogs.com/jayhou/p/12264183.html
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接。