1. 程式人生 > >(轉)Ubuntu中Git伺服器搭建

(轉)Ubuntu中Git伺服器搭建

git伺服器搭建過程

參考網上資料搭建git伺服器過程記錄 如下:

基本需求
硬體需求:一臺Ubuntu或者debian電腦(虛擬機器),能通過網路訪問到。
軟體需求:git-core, gitosis, openssh-server, openssh-client, Apache2(Gitweb)
安裝配置git伺服器 1. 安裝git和openssh:

[email protected]:~$ sudo apt-get install git-core openssh-server openssh-client

新加使用者git, 該使用者將作為所有程式碼倉庫和使用者許可權的管理者:

[email protected]:~$ sudo useradd -m git

[email protected]:~$ sudo passwd git

建立一個git倉庫的儲存點:

[email protected]:~$ sudo mkdir /home/repo

讓除了git以外的使用者對此目錄無任何許可權:

[email protected]:~$ sudo chown git:git /home/repo

[email protected]:~$ sudo chmod 700 /home/repo

2. 安裝配置gitosis

初始化一下的git使用者,這一步其實是為了安裝gitosis做準備。在任何一臺機器上使用git,第一次最好要初始化一下:

[email protected]:~$ git config --global user.name "myname"

[email protected]:~$ git config --global user.email "[email protected]"

安裝一下python的setup tool, 這個也是為了gitosis做準備:

[email protected]:~$ sudo apt-get install python-setuptools

獲得gitosis包:

[email protected]:~$ cd /tmp

[email protected]

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

[email protected]:/tmp$ cd gitosis

[email protected]:/tmp/gitosis$ sudo python setup.py install

切換到git使用者下:

[email protected]:/tmp/gitosis$ su git

預設狀態下,gitosis會將git倉庫放在 git使用者的home下,所以我們做一個連結到/home/repo

$ ln -s /home/repo /home/git/repositories

再次返回到預設使用者

$ exit

如果你將作為git伺服器的管理員,那麼在你的電腦上(另一臺pc)生成ssh公鑰:

[email protected]:~$ ssh-keygen -t rsa

將公鑰拷貝到伺服器的/tmp下:

[email protected]:~$ scp .ssh/id_rsa.pub [email protected]<server>:/tmp

回到git伺服器上

[email protected]:/tmp/gitosis$ sudo chmod a+r /tmp/id_rsa.pub

讓gitosis執行起來:

[email protected]:/tmp/gitosis$ sudo -H -u git gitosis-init < /tmp/id_rsa.pub

Initialized empty Git repository in /home/repo/gitosis-admin.git/

Reinitialized existing Git repository in /home/repo/gitosis-admin.git/

gitosis的有趣之處在於,它通過一個git倉庫來管理配置檔案,倉庫就放在了/home/repo/gitosis-admin.git。

我們需要為一個檔案加上可執行許可權:

[email protected]:/home/git$ sudo passwd root

[email protected]:/home/git$ su

[email protected]:/home/git# cd repositories

[email protected]:/home/git/repositories# cd gitosis-admin.git/

[email protected]:/home/git/repositories/gitosis-admin.git# sudo chmod 755 /home/repo/gitosis-admin.git/hooks/post-update

[email protected]:/home/git/repositories/gitosis-admin.git# exit

3. 在伺服器上新建一個測試專案倉庫

我建了一個叫“teamwork”的倉庫。 
切換到git使用者: 
[email protected]:/home/git$ su git 
$ cd ~/repositories 
$ mkdir teamwork.git 
$ cd teamwork.git 
$ git init --bare 
$ exit

但是,到目前為止,這只是一個空倉庫,空倉庫是不能clone下來的。為了能做clone,我們必須先讓某個有許可權的人放一個初始化的版本到倉庫中。

所以,我們必須先修改一下gitosis-admin.

4. 管理gitosis的配置檔案

剛剛提到,gitosis本身的配置也是通過git來實現的。在你自己的開發機裡,把gitosis-admin.git這個倉庫clone下來,就可以以管理員的身份修改配置了。

在你的電腦裡: 
[email protected]:~/work$ git clone [email protected]&lt;server>:gitosis-admin.git

如果出現:

fatal: '~/gitosis-admin.git' does not appear to be a git repository 
fatal: The remote end hung up unexpectedly

改成:

[email protected]:~/work$ sudo git clone [email protected]<server>:/home/repo/gitosis-admin.git

[email protected]:~/work$ cd gitosis-admin/

該目錄下的keydir目錄是用來存放所有需要訪問git伺服器的使用者的ssh公鑰:

各個使用者按照前面提到的辦法生成各自的ssh公鑰檔案後,把所有人的 ssh公鑰檔案都拿來,

按名字命名一下,比如b.pub, lz.pub等,統統拷貝到keydir下:

[email protected]:~/work/gitosis-admin$ su root

[email protected]:/home/a/work/gitosis-admin # cp /path/to/.ssh/id_rsa.pub ./keydir/b.pub

[email protected]:/home/a/work/gitosis-admin # exit

修改gitosis.conf檔案,我的配置大致如下:

[gitosis]

[group gitosis-admin]

writable = gitosis-admin

members = [email protected] [email protected]

[group team]

writable = teamwork

members = [email protected] b

[group team_ro]

readonly = teamwork

members = lz

這個配置檔案表達瞭如下含義:gitosis-admin組成員有a, usr,該組對gitosis-admin倉庫有讀寫許可權;

team組有a,b兩個成員,該組對teamwork倉庫有讀寫許可權;

team_ro組有lz一個成員,對teamwork倉庫有隻讀許可權。

當然目前這些配置檔案的修改只是在你的本地,你必須推送到遠端的gitserver上才能真正生效。

加入新檔案、提交併push到git伺服器:

[email protected]:~/work/gitosis-admin$ git add .

[email protected]:~/work/gitosis-admin$ git commit -am "add teamwokk prj and users"

[email protected]:~/work/gitosis-admin$ git push origin master

5. 初始化測試專案

好了,現在伺服器就搭建完了,並且有一個空的專案teamwork在伺服器上。接下來呢?

當然是測試一下,空倉庫是不能clone的,所以需要某一個有寫許可權的人初始 化一個版本。就我來做吧,以下是在客戶端完成。

[email protected]:~/work$ mkdir teamwork-ori

[email protected]:~/work$ cd teamwork-ori/

[email protected]:~/work/teamwork-ori$ git init

[email protected]:~/work/teamwork-ori$ echo "/*add something*/" &gt; hello

[email protected]:~/work/teamwork-ori$ git add .

[email protected]:~/work/teamwork-ori$ git commit -am "initial version"

[email protected]:~/work/teamwork-ori$ git remote add origin [email protected]<server>:teamwork.git

[email protected]:~/work/teamwork-ori$ git push origin master

到此為止teamwork已經有了一個版本了,team的其他成員只要先clone一下 teamwork倉庫,就可以任意玩了。

[email protected]:~/work/teamwork-ori$ su b

$ cd /home/b

$ git clone [email protected]<server>:teamwork.git

$ cd teamwork

$ vim hello

$ git add .

$ git commit -am "b add"

$ git push origin master

$ exit

6. 新增已有git專案

另外:如果你有一個現成的git倉庫,想放到 git server上供team使用(比如你clone了一個官方的kernel倉庫,想在內部使用它作為基礎倉庫),怎麼辦呢?

首先需要從你的工作倉庫中得到一個純倉庫, 比如你的工作目錄為~/kernel, 你想匯出純倉庫到你的優盤裡,然後拷貝到git server上去。

$ git clone --bare ~/kernel  /media/udisk

然後就拿著優盤,交給git server的管理員,讓他拷貝到/home/repo/下,同時需要修改gitosis相關配置檔案哦,這個就不用再說了吧。

比如:下載ALSA庫:

git clone git://android.git.kernel.org/platform/external/alsa-lib.git

git clone git://android.git.kernel.org/platform/external/alsa-utils.git

生成bare庫

git clone --bare alsa-lib alsa-lib.git

git clone --bare alsa-utils alsa-utils.git

將bare 庫移動到git伺服器目錄

cp -r alsa-lib.git /home/repo/

注意變更所有者,以獲取提交許可權。

chown -R git alsa-lib.git

然後就OK 了,呵呵.

配置gitweb 1. 安裝gitweb

sudo apt-get install gitweb

2. 安裝apache2

sudo apt-get install apache2

3. 配置gitweb

(1)預設沒有 css 載入,把 gitweb 要用的靜態檔案連線到 DocumentRoot 下:

cd /var/www/

sudo ln -s /usr/share/gitweb/* .

(注意後面的點)

(2)修改配置:

sudo vi /etc/gitweb.conf

將 $projectroot 改為gitosis-admin.git所在目錄: /home/git/repositories

(3)修改 /home/git/repositories許可權,預設情況下,gitosis將 repositories許可權設定為不可讀的

sudo chmod 777 -R /home/git/repositories

4.編輯apache2配置檔案,建立web站點 (預設情況下可以忽略此步驟)

(1) 編輯apache2配置檔案

    ubuntu中預設的web目錄是/var/www,預設的cgi目錄是 /usr/lib/cgi-bin/,安裝完成gitweb後,gitweb的gitweb.cgi會自動放置到該目錄下。

如果你的cgi路徑不是預設的/usr/lib/cgi-bin/,需要將gitweb安裝在/usr/lib/cgi-bin中的gitweb.cgi複製到原來配置的cgi-bin路徑,

並修改apache的配置檔案/etc/apache2/apache.conf:

SetEnv  GITWEB_CONFIG   /etc/gitweb.conf 
    gitweb.conf配置檔案形如:(可自行修改,這裡不做詳細介紹) 
<Directory "/srv/www/cgi-bin/gitweb">           
      Options FollowSymlinks ExecCGI          
      Allow from all                          
      AllowOverride all                       
      Order allow,deny                        
      <Files gitweb.cgi> 
           SetHandler cgi-script 
      </Files>                    
      RewriteEngine on 
      RewriteCond %{REQUEST_FILENAME} !-f 
      RewriteCond %{REQUEST_FILENAME} !-d 
      RewriteRule ^.* /gitweb.cgi/$0 [L,PT] 
</Directory>

(2)重新啟動apache:sudo /etc/init.d/apache2 restart,訪問http://localhost/cgi-bin/gitweb.cgi

<以下未經測試>

配 置web訪問方式:

Apache常用命令: 
a2dissite gitserver 禁用 
a2ensite gitserver  使能 
/etc/init.d/apache2 restart 重啟

1.apt-get install apache2

2.手動安裝gitweb 
git clone git://git.kernel.org/pub/scm/git/git.git 
cd git 
make GITWEB_PROJECTROOT="/home/repo" prefix=/usr gitweb/gitweb.cgi 
cd gitweb 
cp -av git* /home/repo/

3.vim /etc/apache2/sites-available/gitserver 
<VirtualHost 172.20.146.39:80> 
ServerName 172.20.146.39 
DocumentRoot /home/repo 
ScriptAlias /cgi-bin/ /home/repo 
<Directory /home/repo> 
Options ExecCGI +FollowSymLinks +SymLinksIfOwnerMatch 
AllowOverride All 
order allow,deny 
Allow from all 
AddHandler cgi-script cgi 
DirectoryIndex gitweb.cgi 
</Directory> 
</VirtualHost>

4.賦予許可權,很重要:

chgrp -R www-data /home/repo 
chmod a+r repo 
chmod a+x repo

mv hooks/post-update.sample hooks/post-update

5.a2ensite gitserver


6./etc/init.d/apache2 restart

匿名訪問方式: 
git clone http://192.168.1.1/alsa-lib.git 
git clone http://192.168.1.1/alsa-utils.git 
git訪問方式: 
git clone [email protected]:alsa-lib.git 
Web網頁瀏覽: 
http://192.168.1.1

遇到的問題: 
1.windows檔案命名不區分大小 寫,而linux支援。這樣android原始碼下載時會出現一下問題。大約有15個檔案存在這個問題。 
2.庫的描述檔案在.git資料夾的description檔案中。編輯該檔案,在gitweb頁中就會有 description。 
3.gitosis庫hooks下的post- update不是由post-update.sample重新命名過來的,它們不一樣。post-update可以更新工作目錄,保持與庫一致。沒有它配置 檔案是不會更新的。 
4.(1)[email protected]:/home/git$ git add . 
error: readlink(“external/openssl/apps/md4.c”): No such file or directory 
error: unable to index file external/openssl/apps/md4.c 
fatal: adding files failed

(2)[email protected]/external/openssl# git init 
Initialized empty Git repository in /external/openssl/.git/ 
[email protected]/external/openssl # git add . 
error: readlink(“apps/md4.c”): No such file or directory 
error: unable to index file apps/md4.c 
fatal: adding files failed 
(3) [email protected]_r2$ rm -Rf .repo 
[email protected]_r2 $ find . -name “.git” | xargs rm -Rf

相關推薦

UbuntuGit伺服器搭建

git伺服器搭建過程 參考網上資料搭建git伺服器過程記錄 如下: 基本需求 硬體需求:一臺Ubuntu或者debian電腦(虛擬機器),能通過網路訪問到。 軟體需求:git-core, gitosis, openssh-server, openssh-client, Apa

UbuntuGit伺服器搭建

配置gitweb 1. 安裝gitweb   sudo apt-get install gitweb 2. 安裝apache2   sudo apt-get install apache2 3. 配置gitweb (1)預設沒有 css 載入,把 gitweb 要用的靜態檔案連線到 Docum

OL2設置鼠標的樣式

format csdn 移動 初始 pbo led doc 代碼 detail http://blog.csdn.net/gisshixisheng/article/details/49496289 概述: 在OL2中,鼠標默認是箭頭,地圖移動時,鼠標樣式是移動樣式;很

Scala協變(+)、逆變(-)、上界(<:)、下界(>:)簡單介紹

定義類 word ref 一個 pla 而不是 關系 repl 協變 看源碼的時候看到: trait ExtensionId[T <: Extension] {沒見過這個符號啊<: Scala上界(<:)和下界(>:) 1) U >: T

AndroidParcelable接口用法

string date 場景 應用 用法 反序列化 數組 auth 序列化對象 1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parce

Java 關於String的空對象(null) ,空值(empty),空格

空值 ise als 內存 ati 調用 ext cor under 原文出處:Java 中關於String的空對象(null) ,空值(empty),空格 定義 空對象: String s = null; 空對象是指定義一個對象s,但是沒有給該對象分配空間,即沒有實例化

JavaScript==和===的區別

嚴格 布爾型 就是 相同 script scrip === bsp true == 用於比較 判斷 兩者相等 ==在比較的時候可以轉自動換數據類型 ===用於嚴格比較 判斷兩者嚴格相等 ===嚴格比較,不會進行自動轉換,要求進行比較的操作數

Python如何理解if __name__ == '__main__'

面向 知識 about main 想要 二進制 imp space 包導入 摘要 通俗的理解 __name__ == ‘__main__‘ :假如你叫李凱.py,在朋友眼中,你是李凱( __name__ == ‘李凱‘ );在你自己眼中,你是你自己( __name__ ==

Linux設置服務自啟動的三種方式

情況下 level httpd sysv kconfig clas mage com ssh 有時候我們需要Linux系統在開機的時候自動加載某些腳本或系統服務 主要用三種方式進行這一操作: ln -s 在/etc/rc.d/rc

Linux /etc/profile、~/.bash_profile 環境變量配置及執行過程

行修改 你在 關系 轉載 登錄用戶 後者 nbsp inux 第一個 環境變量是和Shell緊密相關的,用戶登錄系統後就啟動了一個Shell。對於Linux來說一般是bash,但也可以重新設定或切換到其它的 Shell。對於UNIX,可能是CShelll。環境變量是通過Sh

ArcGIS如何將大量座標點按順序連線成線或面

ArcGIS中如何將大量座標點按順序連線成線                 工作過程中,如果獲

Linux /etc/profile、~/.bash_profile 環境變數配置及執行過程

環境變數是和Shell緊密相關的,使用者登入系統後就啟動了一個Shell。對於Linux來說一般是bash,但也可以重新設定或切換到其它的 Shell。對於UNIX,可能是CShelll。環境變數是通過Shell命令來設定的,設定好的環境變數又可以被所有當前使用者所執行的程式所使用。對於bash這個Shell

JAVA的堆疊

                        &nb

ubuntu-restricted-extras

來源: http://blog.sina.com.cn/s/blog_59e0c16f0100sndj.html 說法1:ubuntu-restricted-extras包中,包括了一堆Ubuntu 9.1

Python的X[:,0]、X[:,1]、X[:,:,0]、X[:,:,1]、X[:,m:n]和X[:,:,m:n]

      Python中對於陣列和列表進行切片操作是很頻繁的,當然對於切片的操作可供我們直接使用的函式也是很遍歷了,我們今天主要簡單總結一下常用集中索引化方式,希望對大家有所幫助吧。           對於列表的切片比較簡單,在我之前的部落格裡面有詳細的講解,需要的話可

URL的hash井號

1.#的含義 #代表網頁中的一個位置,其右邊的字元,就是該位置的識別符號。比如 http://www.example.com/index.html#print 就是代表index.html中的print位置。瀏覽器會自動把print位置滾動到頁面可視區域內。 設定

Eclipse快速輸入System.out.println()的快捷鍵

  https://blog.csdn.net/ShiMengRan107/article/details/73614417 善用 Eclipse 組合鍵,可以提高輸入效率。 Step1: Ec

Threadyield方法

先上一段程式碼 public class YieldExcemple { public static void main(String[] args) { Thread threada = new ThreadA(); Thread threadb = new Th

Java的守護執行緒 Java的守護執行緒與非守護執行緒

Java的守護執行緒與非守護執行緒   守護執行緒與非守護執行緒 最近在看多執行緒的Timer章節,發現運用到了守護執行緒,感覺Java的基礎知識還是需要補充。 Java分為兩種執行緒:使用者執行緒和守護執行緒 所謂守護執行緒是指在程式執行的時候在後臺提供一

WORD小寫金額轉換成大寫

轉自:http://www.officefans.net/cdb/viewthread.php?tid=52631 '* +++++++++++++++++++++++++++++ '* Created By [email protected] 2007-2-1