眾所周知,版本系統(tǒng)在開發(fā)環(huán)境中是必不可少的,但是我們可以把代碼免費的托管到GitHub上,如果我們不原意公開項目的源代碼,公司又不想付費使用,那么我們可以自己搭建一臺Git服務器,可以用Gitosis來管理公鑰,還是比較方便的。
搭建環(huán)境:
服務器 CentOS6.6 + git(version 1.8.3.1)
客戶端 Windows10 + git(version 2.11.1.windows.1)
1. 安裝Git相關軟件
Linux是服務器端系統(tǒng),Windows作為客戶端系統(tǒng),分別安裝Git
安裝服務端:
1
2
3
|
[root@linuxprobe ~] # yum install -y git [root@localhost ~] # git --version //安裝完后,查看 Git 版本 git version 1.8.3.1 |
安裝客戶端:
下載 Git for Windows,地址:https://git-for-windows.github.io/
安裝完之后,可以使用Git Bash作為命令行客戶端。
1
2
|
$ git --version git version 2.11.1.windows.1 // 安裝完之后,查看Git版本 |
安裝Gitosis
1
2
3
4
5
|
[root@linuxprobe ~] # cd software/ [root@linuxprobe software] # git clone https://github.com/res0nat0r/gitosis.git [root@linuxprobe software] # yum install python-setuptools -y [root@linuxprobe software] # cd gitosis [root@linuxprobe gitosis] # sudo python setup.py install |
出現(xiàn)下面的信息表示安裝成功了
1
2
|
Using /usr/lib/python2.6/site-packages Finished processing dependencies for gitosis==0.2 |
2. 服務器端創(chuàng)建git用戶來管理Git服務
1
2
3
4
5
|
[root@linuxprobe ~] # id git //查看git用戶是否存在 id : git: no such user [root@linuxprobe ~] # useradd git [root@linuxprobe ~] # echo "123" | passwd --stdin git [root@linuxprobe ~] # su - git //切換到git用戶下 |
3. 配置公鑰
在Windows上配置管理者,git服務器需要一些管理者,通過上傳開發(fā)者機器的公鑰到服務器,添加成為git服務器的管理者,打開git命令行
1
2
|
$ ssh -keygen -t rsa // 一直回車,不需要設置密碼 |
4. 配置gitosis
使用git用戶并初始化gitosis
1
2
3
4
5
|
[root@linuxprobe ~] # cd .ssh [root@linuxprobe ~] # gitosis-init < ./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/ [root@linuxprobe ~] # chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update //添加權限 |
在Windows上機器上clone gitosis-admin到管理者主機
1
2
3
4
|
$ git clone ssh : //git @192.168.34.184:22 /gitosis-admin .git $ cd gitosis-admin $ ls $ gitosis.conf keydir |
gitosis.conf: git
服務器配置文件
keydir:
存放客戶端公鑰
配置gitosis.conf文件
1
2
3
4
5
6
7
8
9
10
|
$ vim gitosis.conf [gitosis] [group gitosis-admin] #組名稱 members = yueyong@SHA2-001 #組成員 writable = gitosis-admin #項目名稱 [group test ] // 這里添加了 "test" 項目組,上傳到個git服務器 members = yueyong@SHA2-001 writable = test |
在Windows管理者機器上創(chuàng)建本地test倉庫,并上傳到git服務端
1
2
3
4
5
6
|
$ git config --global user.name "Your Name" // 第一次提交需要設置個人信息,設置用戶名和郵箱 $ git config --global user.email "[email protected]" $ cd ~ /repo $ mkdir test $ git init $ tocuh readme.txt |
提交到遠程服務器
1
2
3
4
|
$ git add . $ git commit -a -m 'init test' $ git push repo master // 上傳本地所有分支代碼到遠程對應的分支上 |
服務端會自動創(chuàng)建test倉庫
1
2
3
4
|
[git@repositories] # pwd /home/git/repositories [git@linuxprobe repositories]$ ls gitosis-admin.git test .git |
5.添加其他git用戶開發(fā)者
由于公司開發(fā)團隊人數(shù)不斷增多,手動添加開發(fā)者私鑰到/home/git/.ssh/authorized_keys比較麻煩,通過上面的Windows機器的管理者統(tǒng)一收集其他開發(fā)者的私鑰id_rsa.pub文件,然后傳到服務器上,配置好后,用戶即獲得項目權限,可以從遠程倉庫拉取和推送項目,達到共同開發(fā)項目。
1
2
3
4
5
6
|
$ cd ~ /gitosis-admin/keydir $ vim gitosis.conf [group test ] writable = test members = yueyong@SHA2-001 zhangsan@SHA2-002 // 添加成員 |
1
2
3
|
$ git add . $ git commit -m "add zhangsan@SHA2-002 pub and update gitosis.conf" $ git push repo master |
推送完成后,新加進來的開發(fā)者就可以進行項目的開發(fā)了,后續(xù)增加人員可以這樣添加進來,開發(fā)者直接把倉庫clone下來就可以了。
1
|
|
報錯問題:ERROR:gitosis serve main repository read access denied
根據(jù)這個報錯,可以看出key是沒問題的,通過排查,發(fā)現(xiàn)不應該把這個/home/git/repositories/test.git寫全,git clone [email protected]:test.git
這樣就可以了。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.linuxprobe.com/linux-deploy-git.html