转载:http://blog.chinaunix.net/u/22630/showart_2190222.html
参考网上资料搭建git服务器过程记录如下:
硬件需求:一台linux Ubuntu电脑(虚拟机),在公司局域网内有独立IP,并且保证小组每个人都能ping通;
软件需求:git-core, gitosis, openssh-server, openssh-client
软件需求:git-core, gitosis, openssh-server, openssh-client
安装git和openssh:
a@ubuntu:~$ sudo apt-get install git-core
a@ubuntu:~$ sudo apt-get install openssh-server
a@ubuntu:~$ sudo apt-get install openssh-client
a@ubuntu:~$ sudo apt-get install git-core
a@ubuntu:~$ sudo apt-get install openssh-server
a@ubuntu:~$ sudo apt-get install openssh-client
建立一个git仓库的存储点,我放在了/home/prj_git下,并且让除了git以外的用户对此目录无任何权限:
a@ubuntu:~$ sudo mkdir /home/prj_git
a@ubuntu:~$ sudo chown git:git /home/prj_git
a@ubuntu:~$ sudo chmod 700 /home/prj_git
a@ubuntu:~$ sudo mkdir /home/prj_git
a@ubuntu:~$ sudo chown git:git /home/prj_git
a@ubuntu:~$ sudo chmod 700 /home/prj_git
初始化一下服务器的git用户,这一步其实是为了安装gitosis做准备,当然在任何一台机器上使用git,第一次必须要初始化一下,git向来不搞“知名不具”那一套:
a@ubuntu:~$ git config --global user.name "ly44770"
a@ubuntu:~$ git config --global user.email " ly44770@163.com "
获得gitosis包:
a@ubuntu:~$ cd /tmp
a@ubuntu:/tmp$ git clone git://eagain.net/gitosis.git
a@ubuntu:/tmp$ cd gitosis
a@ubuntu:/tmp/gitosis$ sudo python setup.py install
a@ubuntu:~$ cd /tmp
a@ubuntu:/tmp$ git clone git://eagain.net/gitosis.git
a@ubuntu:/tmp$ cd gitosis
a@ubuntu:/tmp/gitosis$ sudo python setup.py install
默认状态下,gitosis会将git仓库放在git用户的home下,所以我们做一个链接到/home/prj_git
$ ln -s /home/prj_git /home/git/repositories
$ ln -s /home/prj_git /home/git/repositories
再次返回到默认用户
$ exit
$ exit
如果你将作为git服务器的管理员,那么在你的电脑上(另一台pc)生成ssh公钥:
a@ubuntu:~$ ssh-keygen -t rsa
将公钥拷贝到服务器的/tmp下,并给其他人以读权限:
a@ubuntu:~$ scp .ssh/id_rsa.pub git@192.168.1.39:/tmp
git@192.168.1.39's password:
id_rsa.pub 100% 390 0.4KB/s 00:00
让gitosis运行起来:
a@ubuntu:/tmp/gitosis$ sudo -H -u git gitosis-init < /tmp/id_rsa.pub
Initialized empty Git repository in /home/prj_git/gitosis-admin.git/
Reinitialized existing Git repository in /home/prj_git/gitosis-admin.git/
a@ubuntu:/tmp/gitosis$ sudo -H -u git gitosis-init < /tmp/id_rsa.pub
Initialized empty Git repository in /home/prj_git/gitosis-admin.git/
Reinitialized existing Git repository in /home/prj_git/gitosis-admin.git/
gitosis的有趣之处在于,它通过一个git仓库来管理配置文件,仓库就放在了/home/prj_git/gitosis-admin.git。我们需要为一个文件加上可执行权限:
a@ubuntu:/home/git$ sudo passwd root
a@ubuntu:/home/git$ su
root@ubuntu:/home/git # cd repositories
root@ubuntu:/home/git/repositories # cd gitosis-admin.git/
root@ubuntu:/home/git/repositories/gitosis-admin.git # sudo chmod 755 /home/prj_git/gitosis-admin.git/hooks/post-update
root@ubuntu:/home/git/repositories/gitosis-admin.git # exit
a@ubuntu:/home/git$ sudo passwd root
a@ubuntu:/home/git$ su
root@ubuntu:/home/git # cd repositories
root@ubuntu:/home/git/repositories # cd gitosis-admin.git/
root@ubuntu:/home/git/repositories/gitosis-admin.git # sudo chmod 755 /home/prj_git/gitosis-admin.git/hooks/post-update
root@ubuntu:/home/git/repositories/gitosis-admin.git # exit
我们在服务器上新建一个空的项目仓库供大家测试一下,我建了一个叫“teamwork”的仓库。
切换到git用户:
a@ubuntu:/home/git$ su - git
$ cd /home/prj_git
$ mkdir teamwork.git
$ cd teamwork.git
$ git init --bare
$ exit
切换到git用户:
a@ubuntu:/home/git$ su - git
$ cd /home/prj_git
$ mkdir teamwork.git
$ cd teamwork.git
$ git init --bare
$ exit
在你自己的电脑里,把gitosis-admin.git这个仓库clone下来,这样你就可以以管理员的身份修改配置了。
在你的电脑里:
a@ubuntu:~/work$ git clone git@192.168.1.39:gitosis-admin.git
Initialized empty Git repository in /home/a/work/gitosis-admin/.git/
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 0), reused 5 (delta 0)
Receiving objects: 100% (5/5), done.
a@ubuntu:~/work$
cd gitosis-admin/
为了测试添加一个新用户:
a@ubuntu:~/work/gitosis-admin$ sudo useradd -m b
a@ubuntu:~/work/gitosis-admin$ sudo passwd b
a@ubuntu:~/work/gitosis-admin$ sudo useradd -m b
a@ubuntu:~/work/gitosis-admin$ sudo passwd b
现在把你们team所有人的ssh公钥文件都拿来,按名字命名一下,比如b.pub, lz.pub等,统统拷贝到keydir下:
a@ubuntu:~/work/gitosis-admin$ su root
root@ubuntu:/home/a/work/gitosis-admin # cp /home/b/.ssh/id_rsa.pub ./keydir/b.pub
a@ubuntu:~/work/gitosis-admin$ cp /tmp/lz.pub ./keydir/
root@ubuntu:/home/a/work/gitosis-admin # exit
a@ubuntu:~/work/gitosis-admin$ su root
root@ubuntu:/home/a/work/gitosis-admin # cp /home/b/.ssh/id_rsa.pub ./keydir/b.pub
a@ubuntu:~/work/gitosis-admin$ cp /tmp/lz.pub ./keydir/
root@ubuntu:/home/a/work/gitosis-admin # exit
修改gitosis.conf文件,我的配置大致如下:
[gitosis]
[gitosis]
[group gitosis-admin]
writable = gitosis-admin
members = a@ubuntu
writable = gitosis-admin
members = a@ubuntu
[group team_ro]
readonly = teamwork
members = lz
readonly = teamwork
members = lz
这个配置文件表达了如下含义:gitosis-admin组成员有a,该组对gitosis-admin仓库有读写权限;
team组有a,b两个成员,该组对teamwork仓库有读写权限;
team_ro组有lz一个成员,对teamwork仓库有只读权限。
team组有a,b两个成员,该组对teamwork仓库有读写权限;
team_ro组有lz一个成员,对teamwork仓库有只读权限。
当然目前这些配置文件的修改只是在你的本地,你必须推送到远程的gitserver上才能真正生效。
加入新文件、提交并push到git服务器:
a@ubuntu:~/work/gitosis-admin$ git add .
a@ubuntu:~/work/gitosis-admin$ git commit -am "add teamweok prj and users"
a@ubuntu:~/work/gitosis-admin$ git push origin master
加入新文件、提交并push到git服务器:
a@ubuntu:~/work/gitosis-admin$ git add .
a@ubuntu:~/work/gitosis-admin$ git commit -am "add teamweok prj and users"
a@ubuntu:~/work/gitosis-admin$ git push origin master
好了,现在服务器就搭建完了,并且有一个空的项目teamwork在服务器上。接下来呢?当然是测试一下,空仓库是不能clone的,所以需要某一个有写权限的人初始化一个版本。就我来做吧,以下是在客户端完成。
a@ubuntu:~/work$
mkdir teamwork-ori
a@ubuntu:~/work$ cd teamwork-ori/
a@ubuntu:~/work/teamwork-ori$ git init
a@ubuntu:~/work$ cd teamwork-ori/
a@ubuntu:~/work/teamwork-ori$ git init
a@ubuntu:~/work/teamwork-ori$
echo "/*add something*/" > hello
a@ubuntu:~/work/teamwork-ori$
git add .
a@ubuntu:~/work/teamwork-ori$ git commit -am "initial version"
a@ubuntu:~/work/teamwork-ori$ git commit -am "initial version"
a@ubuntu:~/work/teamwork-ori$
git remote add origin
git@192.168.1.39:teamwork.git
a@ubuntu:~/work/teamwork-ori$ git push origin master
a@ubuntu:~/work/teamwork-ori$ git push origin master
到此为止teamwork已经有了一个版本了,team的其他成员只要先clone一下teamwork仓库,就可以任意玩了。
a@ubuntu:~/work/teamwork-ori$ su b
$ cd /home/b
$ git clone git@192.168.1.39:teamwork.git
$ cd teamwork
$ vim hello
$ git add .
$ git commit -am "b add"
$ vim hello
$ git add .
$ git commit -am "b add"
$ git push origin master
$ exit
$ exit
另外:如果你有一个现成的git仓库,想放到gitserver上供team使用(比如你clone了一个官方的kernel仓库,想在内部使用它作为基础仓库),怎么办呢。
首先需要从你的工作仓库中得到一个纯仓库, 比如你的工作目录为~/kernel, 你想导出纯仓库到你的优盘里,然后拷贝到gitserver上去。
$ git clone --bare ~/kernel /media/udisk
然后就拿着优盘,交给gitserver的管理员,让他拷贝到/home/prj_git/下,同时需要配置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
git clone --bare alsa-lib alsa-lib.git
git clone --bare alsa-utils alsa-utils.git
4.将bare库移动到git服务器目录
cp alsa-lib.git /home/prj_git
5.注意变更所有者,以获取提交权限。
chown -R git alsa-lib.git
然后就O了,呵呵.
配置web访问方式:
Apache常用命令:
a2dissite gitserver 禁用
a2ensite gitserver 使能
/etc/init.d/apache2 restart 重启
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/prj_git" prefix=/usr gitweb/gitweb.cgi
cd gitweb
cp -av git* /home/prj_git/
git clone git://git.kernel.org/pub/scm/git/git.git
cd git
make GITWEB_PROJECTROOT="/home/prj_git" prefix=/usr gitweb/gitweb.cgi
cd gitweb
cp -av git* /home/prj_git/
3.vim /etc/apache2/sites-available/gitserver
<VirtualHost 172.20.146.39:80>
ServerName 172.20.146.39
DocumentRoot /home/prj_git
ScriptAlias /cgi-bin/ /home/prj_git
<Directory /home/prj_git>
Options ExecCGI +FollowSymLinks +SymLinksIfOwnerMatch
AllowOverride All
order allow,deny
Allow from all
AddHandler cgi-script cgi
DirectoryIndex gitweb.cgi
</Directory>
</VirtualHost>
<VirtualHost 172.20.146.39:80>
ServerName 172.20.146.39
DocumentRoot /home/prj_git
ScriptAlias /cgi-bin/ /home/prj_git
<Directory /home/prj_git>
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/prj_git
chmod a+r prj_git
chmod a+x prj_git
chmod a+r prj_git
chmod a+x prj_git
mv hooks/post-update.sample hooks/post-update
5.a2ensite gitserver
6./etc/init.d/apache2 restart
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 git@192.168.1.1:alsa-lib.git
Web网页浏览:
http://192.168.1.1
git clone http://192.168.1.1/alsa-lib.git
git clone http://192.168.1.1/alsa-utils.git
git访问方式:
git clone git@192.168.1.1: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)git@hello:/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
1.windows文件命名不区分大小写,而linux支持。这样android源码下载时会出现一下问题。大约有15个文件存在这个问题。
2.库的描述文件在.git文件夹的description文件中。编辑该文件,在gitweb页中就会有description。
3.gitosis库hooks下的post-update不是由post-update.sample重命名过来的,它们不一样。post-update可以更新工作目录,保持与库一致。没有它配置文件是不会更新的。
4.(1)git@hello:/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)root@/external/openssl# git init
Initialized empty Git repository in /external/openssl/.git/
root@/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) root@android-2.1_r2$ rm -Rf .repo
root@android-2.1_r2 $ find . -name ".git" | xargs rm -Rf
root@android-2.1_r2 $ find . -name ".git"
root@android-2.1_r2 $ git init
Initialized empty Git repository in android-2.1_r2-hopen/.git/
root@android-2.1_r2 $ 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
错误原因:ntfs文件系统不支持symlink。
Initialized empty Git repository in /external/openssl/.git/
root@/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) root@android-2.1_r2$ rm -Rf .repo
root@android-2.1_r2 $ find . -name ".git" | xargs rm -Rf
root@android-2.1_r2 $ find . -name ".git"
root@android-2.1_r2 $ git init
Initialized empty Git repository in android-2.1_r2-hopen/.git/
root@android-2.1_r2 $ 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
错误原因:ntfs文件系统不支持symlink。
(4)gitosis配置中members变量中组成员的使用
## You can use groups just to avoid listing users multiple times. Note
## no writable= or readonly= lines.
## no writable= or readonly= lines.
[group public-group]
members = a b c d xiaoming
members = a b c d xiaoming
## 就是@public-group这样
[group prj1]
writable = prj/build
members = lihua @public-group
writable = prj/build
members = lihua @public-group