Git遠程版本庫

Git遠程版本庫html

===============================================================================node

概述:git


===============================================================================shell

Git 遠程版本庫

1.分佈式版本控制系統
apache

基於的是網絡協議vim

  • http,https,ssh,git
    bash

2.克隆操做
服務器

git clone網絡

原始版本庫存儲在 refs/heads/app

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協議演示:

  1. 在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.conf
 <VirtualHost *:80>
         ServerName 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/  #定義別名的映射路徑
         <Directory "/usr/libexec/git-core/">
                 Options ExecCGI Indexes
                 Require all granted
         </Directory>
 </VirtualHost>
 
[root@node2 git]# httpd -t
Syntax OK

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 <file>..." 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.conf 
<VirtualHost *:80>
        ServerName git.taotao.com
	SetEnv GIT_PROJECT_ROOT /var/www/git
	SetEnv GIT_HTTP_EXPORT_ALL
	ScriptAlias /git/ /usr/libexec/git-core/git-http-backend/
	<Directory "/usr/libexec/git-core/">
	        Options ExecCGI Indexes
		Require all granted
	</Directory>
	<LocationMatch "^/git/.*/git-receive-pack$">
	        AuthType Basic
		AuthName "Private Git Repo"
		AuthUserFile /etc/httpd/conf/.htpasswd
		Require valid-user
        </LocationMatch>
</VirtualHost>

[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(+)

在遠程分支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
相關文章
相關標籤/搜索