XCode 默認支持 Git 作為代碼倉庫,當我們新建一個倉庫的時候,可以勾選創(chuàng)建默認及使用配置,總體思路是:使用 gitosis 來簡化創(chuàng)建過程,在用作服務器的機器上創(chuàng)建一個名為 gitserver 的賬戶來創(chuàng)建 git 本地服務器,其他客戶端通過 ssh 機制訪問 git 服務器。
一、創(chuàng)建新用戶
- 在用作服務器的 Server機器 上創(chuàng)建 gitserver賬戶。我們可以通過 System Preferences->accounts 來添加。在這里我添加一個 gitserver 的 administrator 賬戶,administrator 不是必須的,在這里僅僅為了方便。
或者通過命令:
sudo adduser \
--system \
--shell /bin/sh \
--gecos 'git version control' \
--group \
--disabled-password \
--home /srv/example.com/git \
gitserver
- 設置遠程訪問
logout 當前賬戶,使用 gitserver 賬戶登錄;在 System Preferences->Sharing 中,勾選:Web Sharing 和 Remote Logig。即遠程登錄和文件共享。
二、下載安裝 gitosis
- 下載安裝gitosis
通過以下命令行下載,或者在github上直接下載。
git clone git://eagain.net/gitosis
進入 gitosis 目錄 執(zhí)行 python 腳本來安裝 gitosis。
python setup.py install
三、制作SSH公鑰
- 回到 client 機器上,制作 ssh 公鑰,放置到gitserver的 /tmp 目錄下。使用以下命令。
bash-3.2$ cd ~
bash-3.2$ ssh-keygen -t rsa
bash-3.2$ cd .ssh
bash-3.2$ ls
id_rsa id_rsa.pub
bash-3.2$ cp id_rsa.pub /tmp/yourame.pub
進入到 local 賬戶的 home 目錄,使用 ssh-keygen -t rsa 生成 id_rsa.pub,最后將該文件拷貝放置到 /tmp/yourname.pub,這樣 gitserver 賬戶就可以訪問 yourname.pub了,注意,此處一定要將公鑰的名字改成和yourname.pub中的最后一句相同,比如我的文件中最后一句是yinli@localhost ,因此公鑰名字改為yinli@localhost.pub ,在這里改名是為了便于在 gitserver 中辨識多個 client。
四、使用 ssh 公鑰初始化 gitosis
- 拷貝 yourname.pub 至服務器的 /tmp/yourname.pub。登入服務器機器的 gitserver 賬戶,進入先前提到 gitosis 目錄,進行如下操作初始化 gitosis,初始化完成后,會在 gitserver 的 home 下創(chuàng)建 repositories 目錄。
sudo -H -u gitserver gitosis-init < /tmp/yourname.pub
- 在這里,會將該 公鑰的client 當做認證受信任的賬戶,因此在 gitserver 的 home 目錄下會有記錄,路徑為 /~/.ssh/authorized_keys 的文件內(nèi)容與 yourname.pub 差不多。
open -e ~/.ssh/authorized_keys
我們需要將 authorizd_keys 稍做修改,用文本編輯器打開它,刪除里面的"command="gitosis-serve yourname",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty " 這一行:
- 然后,我們對 post-update 賦予可寫權限,以便 client 端可以提交更改。
sudo chmod 755 /Users/gitserver /repositories//gitosis-admin.git/hooks/post-update
- 最后一步,修改 git 賬戶的 PATH 路徑。
在gitosis目錄下執(zhí)行以下命令
yourname:gitosis git$ touch ~/.bashrc
yourname:gitosis git$ echo PATH=/usr/local/bin:/usr/local/git/bin:\$PATH > .bashrc
yourname:gitosis git$ echo export PATH >> .bashrc
yourname:gitosis git$ cat .bashrc
PATH=/usr/local/bin:/usr/local/git/bin:$PATH
export PATH
五、client 配置
- 回到 local 賬戶,首先在 terminal 輸入如下命令修改 local 的 git 配置:
bash-3.2$ git config --global user.name "yourgitname"
bash-3.2$ git config --global user.email "yourmail@yourcom.com"
與上面類似,此處我的name 填的是 yinli ,email 是 yinli@localhost ,根據(jù)自己的機器來修改。
- 測試服務器是否連接正確,將 10.1.4.211 換成你服務的名稱或服務器地址即可。
ssh gitserver@192.168.1.108
- 在本地 clone 服務器倉庫,下面以 gitosis-admin.git 為例:
bash-3.2$ git clone gitserver@192.168.1.108:repositories/gitosis-admin.git
Cloning into gitosis-admin
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 5 (delta 0), reused 5 (delta 0)
Receiving objects: 100% (5/5), done.
bash-3.2$ ls
Desktop InstallApp Music Sites
Documents Library Pictures gitosis-admin
Downloads Movies Public
bash-3.2$ git
在上面的輸出中可以看到,我們已經(jīng)成功 clone 服務器的 gitosis-admin 倉庫至本地了。
六、添加client,以及新建&配置遠程倉庫
- 進入 gitosis-admin 目錄,我們來查看一下其目錄結構:gitosis.conf 文件是一個配置文件,里面定義哪些用戶可以訪問哪些倉庫,我們可以修改這個配置;keydir 是存放ssh 公鑰的地方。我們可以打開gitosis.conf來看看如何配置gitosis。
[group gitosis-admin]
members = yinli@localhost
writable = gitosis-admin
這個配置文件表達了如下含義:gitosis-admin組成員有yinli@localhost,該組對gitosis-admin倉庫有讀寫權限。因為只添加了一個公鑰,故members成員只有一個。
- 更改并提交本地配置文件
打開gitosis.conf,修改如下。
[group gitosis-admin]
members = yinli@localhost weixxx@localhost
writable = gitosis-admin
[group gitosis-xxprj]
members = yinli@localhost qiao@localhost
writable = teamwork
[group gitosis-xxprj_ro]
readonly = teamwork
members = weixxx@localhost
新建了gitosis-xxprj項目組以及gitosis-xxprj_ro只讀項目組,并加入兩個新的成員qiao@localhost,weixxx@localhost。添加了兩個新的成員,因此必須加入相對應的公鑰,qiao@localhost.pub,weixxx@localhost.pub拷貝并添加到keydir中。
當然目前這些配置文件的修改只是在你的本地,你必須推送到遠程的gitserver上才能真正生效。
加入新文件、提交并push到gitserver服務器:
$ git add .
$ git commit -am “add teamweok prj and users”
$ git push origin master
- 配置文件上傳成功后,我們就需要為gitosis-xxprj等項目添加一個新的遠程倉庫。
我們在服務器上新建一個空的項目倉庫,叫“teamwork”。
切換到gitserver服務器上:
$ cd ~/repositories/
$ mkdir teamwork.git
$ cd teamwork.git
$ git init - -bare 注:這是在服務器上運行的,是為了初始化一個根級的git倉庫
- 初始化項目
好了,現(xiàn)在服務器就搭建完了,并且有一個空的項目teamwork在服務器上。接下來呢?當然是測試一下,空倉庫是不能clone的,所以需要某一個有寫權限的人初始 化一個版本。就我來做吧,以下是在客戶端完成。
$ mkdir teamwork-ori
$ cd teamwork-ori/
$ git init
$ echo “/*add something*/” > hello
$ git add .
$ git commit -am “initial version”
$ git remote add origin gitserver@192.168.1.108:repositories/teamwork.git
$ git push origin master
到此為止teamwork已經(jīng)有了一個版本了,team的其他成員只要先clone一下 teamwork倉庫,就可以任意玩了。