一、基本概述
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协议(DumpSmart)
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
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。