Git远程版本库
成都创新互联是一家集网站建设,都昌企业网站建设,都昌品牌网站建设,网站定制,都昌网站建设报价,网络营销,网络优化,都昌网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
===============================================================================
概述:
===============================================================================
Git 远程版本库
1.分布式版本控制系统
★基于的是网络协议
http,https,ssh,git
2.克隆操作
★git clone
★原始版本库存储在 refs/heads/
3.git 服务器
★协议
本地协议(local)、HTTP/HTTPS协议、SSH协议、Git协议
☉本地协议
URL:
/path/to/repo.git
file:///path/to/repo.git
☉Git协议:由git-daemon程序提供,监听在tcp的9418端口;仅支持“读”操作,无任何认证功能(支持开放式的开源项目);
URL:
git://host/path/to/repo.git
git://host/~user/path/to/repo.git
☉SSH协议
URL:
ssh://[USER@]host[:port]/path/to/repo.git
ssh://[USER@]host[:port]/~USERNAME/path/to/repo.git
URL2:
[USER@]hostpath/torepo.git
☉HTTP/HTTPS协议
1.6.5-:哑 http 协议
1.6.6+:智能 http 协议 (读/写/认证)
URL:
http://host/path/to/repo.git
演示1:本地协议
[root@node1 ~]# git clone file:///root/taotao/ /root/huihui Cloning into '/root/huihui'... remote: Enumerating objects: 32, done. remote: Counting objects: 100% (32/32), done. remote: Compressing objects: 100% (23/23), done. remote: Total 32 (delta 8), reused 0 (delta 0) Receiving objects: 100% (32/32), done. Resolving deltas: 100% (8/8), done. [root@node1 ~]# cd /root/huihui/ [root@node1 huihui]# ls first.sh INSTALL my.txt readmin second.sh subdir [root@node1 huihui]# cd .git/ [root@node1 .git]# ls branches config description HEAD hooks index info logs objects packed-refs refs [root@node1 .git]# tree refs/ refs/ ├── heads │ └── master #实际上只clone的master分支 ├── remotes #远程跟踪分支 │ └── origin │ └── HEAD #指向master分支 └── tags 4 directories, 2 files [root@node1 ~]# tree /root/taotao/.git/refs/ /root/taotao/.git/refs/ ├── heads │ ├── dev │ ├── fotfix │ └── master └── tags 2 directories, 3 files
引用远程版本库
1.分布式版本控制系统
★远程版本库
定义在配置文件中的一个实体;
[remote "NAME"]
☉由两部分组成:
第一部分:URL
第二部分:refspec,定义一个版本库与其他版本库的名称空间的映射关系;
☉语法格式:
+source:destination(本地分支和映射的目标分支)
refs/heads/NAME:本地分支
refs/remotes/NAME:远程跟踪分支
eg:
[remote "publish"]
url=http://HOST/pub/repo_name.git
push= +refs/heads/*:refs/remotes/origin/*
显示样子:
remote.publish.url
remote.publish.push
★git remote 命令可管理远程仓库
git协议演示:
在node2服务器上安装git服务器,并启动服务
[root@node2~]# yum install git-daemon [root@node1 huihui]# rpm -ql git-daemon /usr/lib/systemd/system/git.socket #为一个瞬时守护进程,可以直接启动 /usr/lib/systemd/system/git@.service /usr/libexec/git-core/git-daemon /usr/share/doc/git-daemon-1.8.3.1 /usr/share/doc/git-daemon-1.8.3.1/git-credential-cache--daemon.html /usr/share/doc/git-daemon-1.8.3.1/git-credential-cache--daemon.txt /usr/share/doc/git-daemon-1.8.3.1/git-daemon.html /usr/share/doc/git-daemon-1.8.3.1/git-daemon.txt /usr/share/man/man1/git-credential-cache--daemon.1.gz /usr/share/man/man1/git-daemon.1.gz /var/lib/git #git仓库的存放位置,相当于根目录 [root@node2 ~]# cat /usr/lib/systemd/system/git@.service [Unit] Description=Git Repositories Server Daemon Documentation=man:git-daemon(1) [Service] User=nobody # 修改 --base-path 后的路径可以改变git仓库的根目录 ExecStart=-/usr/libexec/git-core/git-daemon --base-path=/var/lib/git --export-all --user-path=public_git --syslog --inetd --verbose StandardInput=socket #启动服务,并查看tcp的端口9418 [root@node2 ~]# systemctl start git.socket [root@node2 ~]# ss -tnl |grep 9418 LISTEN 0 128 :::9418 :::*
2. 在nod2远程服务器上的git根目录下创建一个裸仓库,无需工作目录
[root@node2 ~]# cd /var/lib/git/ [root@node2 git]# git init --bare myproject.git 初始化空的 Git 版本库于 /var/lib/git/myproject.git/ [root@node2 git]# ls myproject.git/ branches config description HEAD hooks info objects refs
3. 在node1节点克隆node2节点的远程仓库
[root@node1 ~]# git clone git://192.168.0.102/myproject.git Cloning into 'myproject'... warning: You appear to have cloned an empty repository. [root@node1 myproject]# ls -a . .. .git [root@node1 myproject]# git config -l user.name=watao user.email=wangzhangtao@pachiratech.com core.repositoryformatversion=0 core.filemode=true core.bare=false core.logallrefupdates=true remote.origin.url=git://192.168.0.108/myproject.git remote.origin.fetch=+refs/heads/*:refs/remotes/origin/* branch.master.remote=origin branch.master.merge=refs/heads/master
http协议演示:
1.安装httpd,确保有alias,env和cgi模块
[root@node2~]# yum install httpd # 确保有如下3个模块 [root@node2 ~]# httpd -M |grep -Ei "\<(alias|cgi|env)" alias_module (shared) env_module (shared) cgi_module (shared)
2.创建裸仓库,并修改属组和属主为apache用户
[root@node2 ~]# mkdir /var/www/git [root@node2 ~]# cd /var/www/git/ [root@node2 git]# ls [root@node2 git]# git init --bare testproject.git 初始化空的 Git 版本库于 /var/www/git/testproject.git/ [root@node2 git]# chown -R apache.apache /var/www/git/ [root@node2 git]# ll /var/www/git/ 总用量 4 drwxr-xr-x 7 apache apache 4096 11月 5 22:46 testproject.git
3.自定义虚拟主机
[root@node2 git]# vim /etc/httpd/conf/httpd.conf 注释掉 DocumentRoot "/var/www/html" [root@node2 git]# vim /etc/httpd/conf.d/git.confServerName git.taotao.com SetEnv GIT_PROJECT_ROOT /var/www/git #指明git根目录位置 SetEnv GIT_HTTP_EXPORT_ALL #基于http协议导出所有功能 ScriptAlias /git/ /usr/libexec/git-core/git-http-backend/ #定义别名的映射路径 [root@node2 git]# httpd -t Syntax OKOptions ExecCGI Indexes Require all granted
4.启动httpd服务,并在node1节点克隆testproject.git仓库
[root@node2 git]# systemctl start httpd [root@node2 git]# ss -tnl |grep 80 LISTEN 0 128 :::80 :::* remote helper 是 git 执行 git clone http://.. 所需要的一个模块,而这个模块坐落在 git-core 目录里,并且这个模块的文件名叫 git-remote-http [root@node1]# cd /usr/local/git/bin [root@node1 bin]# ln -s /usr/libexec/git-core/git-remote-http /usr/local/git/bin/git-remote-http [root@node1 bin]# ln -s /usr/libexec/git-core/git-remote-https /usr/local/git/bin/git-remote-https [root@node1 bin]# ll total 67600 -rwxr-xr-x 126 root root 15006529 Jul 3 22:26 git -rwxr-xr-x 2 root root 162741 Jul 3 22:26 git-cvsserver -rwxr-xr-x 1 root root 351673 Jul 3 22:26 gitk -rwxr-xr-x 126 root root 15006529 Jul 3 22:26 git-receive-pack lrwxrwxrwx 1 root root 37 Nov 5 23:29 git-remote-http -> /usr/libexec/git-core/git-remote-http lrwxrwxrwx 1 root root 38 Nov 5 23:29 git-remote-https -> /usr/libexec/git-core/git-remote-https -rwxr-xr-x 2 root root 8672864 Jul 3 22:26 git-shell -rwxr-xr-x 126 root root 15006529 Jul 3 22:26 git-upload-archive -rwxr-xr-x 126 root root 15006529 Jul 3 22:26 git-upload-pack [root@node1 ~]# git clone http://192.168.0.108/git/testproject.git Cloning into 'testproject'... warning: You appear to have cloned an empty repository. [root@node1 ~]# cd testproject/ [root@node1 testproject]# ls -a . .. .git [root@node1 testproject]# tree .git/ .git/ ├── branches ├── config ├── description ├── HEAD ├── hooks │ ├── applypatch-msg.sample │ ├── commit-msg.sample │ ├── fsmonitor-watchman.sample │ ├── post-update.sample │ ├── pre-applypatch.sample │ ├── pre-commit.sample │ ├── prepare-commit-msg.sample │ ├── pre-push.sample │ ├── pre-rebase.sample │ ├── pre-receive.sample │ └── update.sample ├── info │ └── exclude ├── objects │ ├── info │ └── pack └── refs ├── heads └── tags 9 directories, 15 files [root@node1 testproject]# git config -l user.name=watao user.email=wangzhangtao@pachiratech.com core.repositoryformatversion=0 core.filemode=true core.bare=false core.logallrefupdates=true remote.origin.url=http://192.168.0.108/git/testproject.git remote.origin.fetch=+refs/heads/*:refs/remotes/origin/* branch.master.remote=origin branch.master.merge=refs/heads/master
5.创建文件并上传远程版本库,发现没有权限
[root@node1 testproject]# echo "New Line" > README [root@node1 testproject]# cat README New Line [root@node1 testproject]# git add README [root@node1 testproject]# git status On branch master No commits yet Changes to be committed: (use "git rm --cached..." to unstage) new file: README [root@node1 testproject]# git commit -m "v0.1" [master (root-commit) fb24458] v0.1 1 file changed, 1 insertion(+) create mode 100644 README [root@node1 testproject]# [root@node1 testproject]# [root@node1 testproject]# git push origin master fatal: unable to access 'http://192.168.0.108/git/testproject.git/': The requested URL returned error: 403 #没有权限
6.要想能够做到允许用户上传操作,需要配置认证访问并开放访问权限
#在node2节点开通http.receivepack [root@node2 testproject.git]# git config http.receivepack true [root@node2 testproject.git]# git config -l core.repositoryformatversion=0 core.filemode=true core.bare=true http.receivepack=true #在node1节点再次上传成功 [root@node1 testproject]# git push origin master Enumerating objects: 3, done. Counting objects: 100% (3/3), done. Writing objects: 100% (3/3), 216 bytes | 72.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To http://192.168.0.108/git/testproject.git * [new branch] master -> master #在node2的远程版本库查看已经有提交对象 [root@node2 testproject.git]# ls branches config description HEAD hooks info objects refs [root@node2 testproject.git]# cd objects/ [root@node2 objects]# ls 6b dc fb info pack [root@node2 objects]# tree . ├── 6b │ └── f181e7944f75a6411a13d94762118dafbc2cff ├── dc │ └── a7b9ab7d0a8f78dd357082f43ddd06c36533ee ├── fb │ └── 244584596a111b649cc0ee0e4e0a554be60c68 ├── info └── pack
7.开放用户认证
[root@node2]# cd /etc/httpd/conf.d [root@node2 conf.d]# cat git.confServerName git.taotao.com SetEnv GIT_PROJECT_ROOT /var/www/git SetEnv GIT_HTTP_EXPORT_ALL ScriptAlias /git/ /usr/libexec/git-core/git-http-backend/ [root@node2 conf.d]# httpd -t Syntax OK [root@node2 ~]# systemctl restart httpd #node1节点删除原来的目录,重新克隆 [root@node1 ~]# rm -fr testproject/ [root@node1 ~]# git clone http://192.168.0.108/git/testproject.git Cloning into 'testproject'... remote: Counting objects: 3, done. remote: Total 3 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. [root@node1 ~]# [root@node1 ~]# cd testproject/ [root@node1 testproject]# ls README #显示本地分支 [root@node1 testproject]# git show-branch [master] v0.1 #显示远程分支 [root@node1 testproject]# git show-branch -r ! [origin/HEAD] v0.1 ! [origin/master] v0.1 -- ++ [origin/HEAD] v0.1 # 新添加一行 [root@node1 testproject]# echo "second line" >> README [root@node1 testproject]# cat README New Line second line [root@node1 testproject]# git add README [root@node1 testproject]# git commit -m "v0.2" [master a9bc92e] v0.2 1 file changed, 1 insertion(+)Options ExecCGI Indexes Require all granted AuthType Basic AuthName "Private Git Repo" AuthUserFile /etc/httpd/conf/.htpasswd Require valid-user
在远程分支node2节点创建两个用户,并在node1本地上传
[root@node2 ~]# htpasswd -c -m /etc/httpd/conf/.htpasswd tom New password: Re-type new password: Adding password for user tom [root@node2 ~]# htpasswd -m /etc/httpd/conf/.htpasswd jerry New password: Re-type new password: Adding password for user jerry [root@node2 ~]# systemctl reload httpd #node1节点上传需要输入密码 [root@node1 testproject]# git push origin master Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Writing objects: 100% (3/3), 254 bytes | 42.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) Username for 'http://192.168.0.108': tom Password for 'http://tom@192.168.0.108': To http://192.168.0.108/git/testproject.git fb24458..a9bc92e master -> master
文章名称:Git远程版本库
文章源于:http://lswzjz.com/article/geiecp.html