git(學習之四)git協議服務器搭建

#######################################################################################################

qq:1218761836

qq羣:150181442

E-mail:[email protected]

#######################################################################################################

目錄

Git 服務器的搭建... 1

1.1 Git協議的服務器搭建... 1

1.1.1 安裝git. 1

1.2.1 創建目錄及版本庫... 1

1.2.3 Git 服務器啓動... 2

1.2.4 客戶端測試... 4

Git 服務器的搭建

遠程倉庫通常只是一個純倉庫(bare repository)—一個沒有當前工作目錄的倉庫。因爲該倉庫只是一個合作媒介,所以不需要從一個處於已從硬盤上檢出狀態的快照;倉庫裏僅僅是git的數據。更簡單的說,純倉庫是你的項目裏的.git內容。

開始架設git服務器的時候,需要把一個現存的倉庫導出爲新的純倉庫—不包含當前工作目錄的倉庫。方法很簡單。把一個倉庫克隆爲純倉庫,可以使用clone命令的--bare選項。純倉庫的目錄名以.git 結尾。

Git服務器搭建根據自己的需求選擇不同的協議

Git支持http:// git:// ssh:// https:// file:// (本地)

ssh://[user@]host.xz[:port]/path/to/repo.git/

git://host.xz[:port]/path/to/repo.git/

http[s]://host.xz[:port]/path/to/repo.git/

ftp[s]://host.xz[:port]/path/to/repo.git/

rsync://host.xz/path/to/repo.git/

1.1 Git協議的服務器搭建

1.1.1 安裝git

安裝git軟件,使用yum安裝的方式

yum install git-* -y 安裝git所有的包

git-all

git-cvs

git-daemon

git-email

git-gui

git-svn

因爲要搭建git協議的服務器,所以git-daemon是必須要安裝的,git-daemon支持兩種啓動方式,一種是git-daemon 的直接啓動方式,一種是利用centos下的xinetd來加載git進程。

1.2.1 創建目錄及版本庫

[root@wx ~]# mkdir /project/git/ -p

[root@wx git]# git init # 創建一個git版本庫

Initialized empty Git repository in /project/git/.git/

[root@wx project]# git clone –bare /project/git/.git/ my-project.git

Initialized empty Git repository in /project/my-project.git/

warning: You appear to have cloned an empty repository.

[root@wx /]# tree /project/my-project.git/

/project/my-project.git/

├── branches

├── config

├── description

├── HEAD

├── hooks

│   ├── applypatch-msg.sample

│   ├── commit-msg.sample

│   ├── post-commit.sample

│   ├── post-receive.sample

│   ├── post-update.sample

│   ├── pre-applypatch.sample

│   ├── pre-commit.sample

│   ├── prepare-commit-msg.sample

│   ├── pre-rebase.sample

│   └── update.sample

├── info

│   └── exclude

├── objects

│   ├── info

│   └── pack

└── refs

├── heads

└── tags

9 directories, 14 files

可以看到這些文件其實和svn的配置文件差不多。

1.2.3 Git 服務器啓動

根據官網提示我使用git daemon --reuseaddr --base-path=/project/ /project/ 運行結果失敗了,其實git守護進程結合系統運行模式有三種,一種是守護進行運行,後兩種是xinetd,sysinit

我選擇了centos 結合xined,所以在centos下可以結合xinetd來加載git服務,其實在安裝git-daemon的時候也會安裝上xined這個包。

Git守護進程的端口是9418

[root@wx /]# grep 9418 /etc/services

git 9418/tcp # git pack transfer service

git 9418/udp # git pack transfer service

來看看默認xined加載的git服務的配置文件,具體可以去man xined 去了解一下xined

[root@wx /]# cat /etc/xinetd.d/git

# default: off

# description: The git d?mon allows git repositories to be exported using \

# the git:// protocol.

service git

{

disable = yes

socket_type = stream

wait = no

user = nobody

server = /usr/libexec/git-core/git-daemon

server_args = --base-path=/var/lib/git --export-all --user-path=public_git --syslog --inetd --verbose

log_on_failure += USERID

}

修完的配置文件

[root@wx /]# vim /etc/xinetd.d/git

# default: off

# description: The git d?mon allows git repositories to be exported using \

# the git:// protocol.

service git

{

disable = no

socket_type = stream

wait = no

user = root

server = /usr/libexec/git-core/git-daemon

server_args = --base-path=/project/my-project.git --export-all --user-path=root --syslog --inetd --verbose

log_on_failure += USERID

}

~

[root@wx /]# /etc/init.d/xinetd restart

Stopping xinetd: [FAILED]

Starting xinetd: [ OK ]

[root@wx /]# netstat -anlt|grep 9418

tcp 0 0 :::9418 :::* LISTEN

1.2.4 客戶端測試

可以看到xined 結合git走的是tcp協議

客戶端測試,客戶端測試的時候只需要安裝git就行

[root@wx-a /]# git clone git://20.0.0.89/my-project.git sadoc

Initialized empty Git repository in /sadoc/.git/

warning: You appear to have cloned an empty repository.

將服務器的my-project.git 克隆到本地的sadoc, 不過在客戶端測試的時候我發現,git clone 在那個目錄下你克隆的服務器版本庫就會是你當前執行git clone的目錄

在服務器端提交一些文件,在客戶端進行下載一下

[root@wx-a /]# cd sadoc/

[root@wx-a sadoc]# git remote -v

origin git://20.0.0.89/my-project.git (fetch)

origin git://20.0.0.89/my-project.git (push)

[root@wx-a sadoc]# git remote

origin

[root@wx-a sadoc]# ls -a

. .. .git

[root@wx-a sadoc]# git remote -v

origin git://20.0.0.89/my-project.git (fetch)

origin git://20.0.0.89/my-project.git (push)

[root@wx-a sadoc]# touch aa

[root@wx-a sadoc]# git add aa

[root@wx-a sadoc]# git status

# On branch master

#

# Initial commit

#

# Changes to be committed:

# (use "git rm --cached <file>..." to unstage)

#

# new file: aa

#

[root@wx-a sadoc]# git commit -m "aa" #提交的時候提示我需要配置用戶名和郵箱

[master (root-commit) 90291de] aa

Committer: root <[email protected]>

Your name and email address were configured automatically based

on your username and hostname. Please check that they are accurate.

You can suppress this message by setting them explicitly:

git config --global user.name "Your Name"

git config --global user.email [email protected]

If the identity used for this commit is wrong, you can fix it with:

git commit --amend --author='Your Name <[email protected]>'

0 files changed, 0 insertions(+), 0 deletions(-)

create mode 100644 aa

參考資料:http://www.git-scm.com/book/en/v2

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