跳轉到內容

Git/Gitosis

來自華夏公益教科書
< Git

Gitosis 是一種工具,用於保護集中式的 Git 倉庫,允許多個維護者同時管理同一個專案,透過限制對只通過安全網路協議的訪問來實現。

安裝 Gitosis

[編輯 | 編輯原始碼]

簽出 Gitosis 倉庫

[編輯 | 編輯原始碼]

要安裝 Gitosis,您首先必須安裝 Git 客戶端。安裝完成後,從其倉庫簽出一個 Gitosis 的副本

 git clone git://eagain.net/gitosis.git

安裝

 cd gitosis
 python setup.py install

建立一個使用者來管理倉庫

[編輯 | 編輯原始碼]

建立一個使用者來管理倉庫

 sudo adduser \
   --system \
   --shell /bin/sh \
   --gecos 'git version control' \
   --group \
   --disabled-password \
   --home /home/git \
   git

如果您還沒有公鑰,請在您的本地計算機上建立一個

 ssh-keygen -t rsa

將您的公鑰複製到 Gitosis 伺服器

[編輯 | 編輯原始碼]

將此金鑰複製到 Gitosis 伺服器。假設您在您的主目錄中

 scp .ssh/id_rsa.pub user@example.com:/tmp

設定 Gitosis

[編輯 | 編輯原始碼]

初始化 Gitosis

[編輯 | 編輯原始碼]

初始化 Gitosis

 sudo -H -u git gitosis-init < /tmp/id_rsa.pub

成功後,您將看到

 Initialized empty Git repository in ./
 Initialized empty Git repository in ./

確保 Git post-update hook 具有正確的許可權

 sudo chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update


配置 Gitosis

[編輯 | 編輯原始碼]

克隆 Gitosis 倉庫

[編輯 | 編輯原始碼]

Gitosis 建立了它自己的 Git 倉庫。要配置 Gitosis,您需要克隆這個倉庫,設定您的配置選項,然後將您的配置推送到 Gitosis 伺服器。

克隆 Gitosis 倉庫

 git clone git@example.com:gitosis-admin.git
 cd gitosis-admin

建立倉庫

[編輯 | 編輯原始碼]

編輯 gitosis.conf

一個預設 gitosis.conf 的示例

 [gitosis]                  
 [group gitosis-admin]
 writable = gitosis-admin
 members = jdoe 

定義組、成員、許可權和倉庫

[編輯 | 編輯原始碼]

您可以像這樣定義成員組以及他們對倉庫的許可權

 [group blue_team]
 members = john martin stephen
 writable = tea_timer coffee_maker

在這個例子中,blue_team 組中的任何成員,在本例中是 john、martin 和 stephen,都將可以寫入 tea_timercoffee_maker Git 倉庫

儲存、提交併推送此檔案。

 git commit -am "Give john, martin, and stephen access to the repositories tea_timer and coffee_maker."
 git push

建立倉庫

[編輯 | 編輯原始碼]

接下來,建立一個倉庫。您需要先切換到您要儲存 Git 倉庫本地副本的目錄。

建立倉庫

 mkdir tea_timer
 cd tea_timer
 git init
 git remote add origin git@example.com:tea_timer.git
 # Add some files and commit.
 git push origin master:refs/heads/master
 # The previous line links your local branch master to the remote branch master so you can automatically fetch and merge with git pull.

將使用者新增到倉庫

[編輯 | 編輯原始碼]

使用者透過他們的公鑰來標識。Gitosis 將這些金鑰儲存在 gitosis-admin 倉庫的 keydir 目錄中。使用者透過金鑰檔案的名稱與其 Git 使用者名稱相關聯。例如,將一個 RSA 金鑰新增到 keydir/john.pub 將會將使用者 john 與 john.pub 中定義的機器相關聯。金鑰必須以 .pub 結尾!

新增使用者

 cd gitosis-admin
 cp /path/to/rsa/key/john.pub keydir/
 git add keydir/*
 git commit -am "Adding the john account."
 git push

John 現在可以克隆他可以訪問的 Git 倉庫,這些倉庫由 gitosis.conf 定義。在本例中,他可以讀取和寫入倉庫,因為他擁有 writable 許可權。

[編輯 | 編輯原始碼]
華夏公益教科書