https://github.com/jackliu2013/recipes/blob/master/doc/linux/CentOS_6.4_git服務器搭建.md
##CentOS安裝Git服務器 Centos 6.4 + Git 1.8.2.2 + gitosis##
1.查看Linux系統服務器系統版本
```
cat /etc/redhat-release # 查看系統版本
CentOS release 6.4 (Final)
ifconfig # 查看服務器的IP
eth0
Link encap:Ethernet HWaddr 00:23:8B:FA:78:92
inet addr:192.168.100.202 Bcast:192.168.100.255 Mask:255.255.255.0
inet6 addr: fe80::223:8bff:fefa:7892/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:543645 errors:0 dropped:0 overruns:0 frame:0
TX packets:157155 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:383527379 (365.7 MiB) TX bytes:13270106 (12.6 MiB)
Interrupt:16
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
```
2.在服務器上安裝git及作些操做
- 執行命令
`
sudo yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel
`
- 同時下載git-1.8.2.2.tar.gz文件,而後將其`mv` 到`/usr/local/src`目錄。[git-1.8.2.2.tar.gz安裝包下載地址][1]
```
cd /usr/local/src
sudo tar -zvxf git-1.8.2.2.tar.gz
cd git-1.8.2.2
sudo make prefix=/usr/local/git all
sudo make prefix=/usr/local/git install
```
- 增長軟鏈接
```
sudo ln -s /usr/local/git/bin/* /usr/bin/
git --version #若是能顯示版本號,即表示成功`
```
3.在服務器安裝gitosis
```
sudo yum install python python-setuptools
cd /usr/local/src
git clone git://github.com/res0nat0r/gitosis.git
cd gitosis
python setup.py install
#顯示Finished processing dependencies for gitosis==0.2即表示成功
```
4.在開發機上,生產密鑰並上傳到服務器上
```
ssh-keygen -t rsa #一路回車,不須要設置密碼
#上傳公鑰到服務器(默認SSH端口22)
scp ~/.ssh/id_rsa.pub tailin@192.168.100.202:/tmp
```
或編輯`/etc/hosts`文件,在`/etc/hosts`文件裏添加以下文本:
```
# local git server
192.168.100.202 zgit
```
而後再上傳本身的公鑰到服務器
```
scp ~/.ssh/id_rsa.pub tailin@zgit:/tmp/
# 登陸到git服務器
ls /tmp/id_rsa.pub #顯示已經上傳的密鑰
```
5.服務器上生成git用戶,使用git用戶並初始化`gitosis`
```
# 建立git版本管理用戶 git
sudo useradd -c 'git version manage' -m -d /home/git -s bin/bash git
# 更改git用戶的密碼
sudo passwd git
# su 到git用戶
su - git
gitosis-init < /tmp/id_rsa.pub
#顯示如下信息即表示成功
#Initialized empty Git repository in /home/git/repositories/gitosis-admin.git/
#Reinitialized existing Git repository in /home/git/repositories/gitosis-admin.git/
#刪除密鑰
rm -rf /tmp/id_rsa.pub
```
6.在我的開發機上導出項目管理
```
mkdir -p /repo
cd /repo
git clone git@zgit:gitosis-admin.git
```
7.在我的開發機增長及設置管理項目
```
cd /repo/gitosis-admin
# 查看git服務器已經上傳密鑰
ls keydir
cat keydir/ltl@jackliu-ThinkPad.pub
#ltl@jackliu-ThinkPad.pub爲已經上傳的開發機生成的公密
#顯示密鑰 最後的字符串爲 密鑰用戶名 這裏爲 ltl@jackliu-ThinkPad
vim gitosis.conf
#在文件尾增長如下內容
[group test-git] # 具備寫權限的組名稱
writable = test-git # 該組可寫的項目名稱
members = ltl@jackliu-ThinkPad guangyun.ni@yeepay.com #該組的成員(密鑰用戶名) 多個用戶協同開發時,以空格分隔
# 若是要增長只讀的組 參考以下
# [group test-git-readnoly] # 具備都權限的組名稱
# readonly = test-git # 該組只讀的項目名稱
# members = ltl@jackliu-ThinkPad # 該組的成員
#提交修改
git add .
git commit -a -m "add test-git repo"
git push
```
8.在我的開發機上初始,增長及使用項目test-git
```
cd ~/repo
mkdir test-git
cd test-git
git init
touch readme
git add .
git commit -a -m "init test-git"
git remote add origin git@zgit:test-git.git
git push origin master
```
9.增長協同開發者的公鑰key到git服務器
- 執行`cd repo/gitosis-admin/keydir`切換目錄
- 把協同開發者的id_rsa.pub 文件裏的數據 拷貝到 對應的開發者的`密鑰用戶名.pub`文件。如把密鑰用戶名 guangyun.ni@yeepay.com 的 id_rsa.pub 文件中文本 粘貼到 guangyun.ni@yeepay.com.pub 文件裏,並保存
- 而後將添加數據後的目錄更新到git服務器
```
git add .
git commit -am "add guangyun.ni@yeepay.com.pub file"
git push origin master
```
本文檔參考:
[CentOS git搭建參考1][2],
[CentOs上搭建git服務器][3]
[1]: http://code.google.com/p/git-core
[2]: http://blog.sina.com.cn/s/blog_86fe5b440101975o.html
[3]: http://www.cnblogs.com/nasa/archive/2012/05/31/2528901.htmljackliu@jackliu-ThinkPad:~/workspace/recipes/doc/linux$html