ubuntu10.04 Git服务器搭建之Gitosis

二:搭建权限管理的GIT服务器Gitosis


①安装相关软件:
sudo apt-get install git-core openssh-server openssh-client
sudo apt-get install git-daemon-run

②然后添加git 用户:
sudo useradd  -m -s /bin/bash -d /home/git git  (-m 自动创建用户目录 /home/git -s 指定登入后所用shell -d 用户登入后所默认进入的目录)
sudo passwd git ---为了以后su git登陆好记密码 我一样用git

③获取并安装gitosis
安装一下python的setup tool, 这个也是为了gitosis做准备:
sudo apt-get install python-setuptools
然后获取并安装gitosis:
ubuntu@server:/pub/tmp$git clone https://github.com/res0nat0r/gitosis.git
下载后安装:
ubuntu@server:/pub/tmp/gitosis$sudo python setup.py install

④配置gitosis
首先固定一个仓库存储路径
ubuntu@server:~$ sudo chown git:git /pub/gittrees
ubuntu@server:~$ sudo chmod 700 /pub/gittrees
ubuntu@server:~$ sudo chmod 700 /pub/gittrees
我们做一个链接到/pub/gittrees(因为/pub挂载点是个单独的硬盘)
ubuntu@server:~$ ln -s /pub/gittrees /home/git/repositories(gitosis启动后/home/git/repositories作为默认仓库)

⑤运行gitosis
如果你将作为git服务器的管理员,那么在你的电脑上(另一台pc)生成ssh公钥:
a@ubuntu:~$ ssh-keygen -t rsa
将公钥拷贝到服务器的/tmp下,并给其他人以读权限:
[mid@MID ~/.ssh]$scp id_rsa.pub [email protected]:/tmp
[email protected]'s password: 
id_rsa.pub                                                                    100%  403     0.4KB/s   00:00    
ubuntu@server:~$sudo chmod a+r /tmp/id_rsa.pub
让gitosis运行起来:
sudo -H -u git gitosis-init < /tmp/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/
ubuntu@server:~$ ls /home/git/ -lh(此时/home/git目录下自动生成repositories目录作为git默认仓库点)
total 4.0K
drwxr-xr-x 2 git git 4.0K 2014-04-22 17:00 gitosis
lrwxrwxrwx 1 git git   13 2014-04-22 17:00 repositories -> /pub/gittrees

⑥管理gitosis配置
gitosis由一个git仓库来管理配置文件,仓库就放在了 /home/git/repositories/gitosis-admin.git/。
[mid@MID ~/work/gitosis]$git clone [email protected]:gitosis-admin.git
正克隆到 'gitosis-admin'...
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 5 (delta 0), reused 5 (delta 0)
接收对象中: 100% (5/5), done.
检查连接... 完成。
[mid@MID ~/work/gitosis]$ls
gitosis-admin
[mid@MID ~/work/gitosis]$cd gitosis-admin/
[mid@MID ~/work/gitosis/gitosis-admin]$ls
gitosis.conf  keydir


⑦添加另外一台PC用户给server
win@Debian:~$scp .ssh/id_rsa.pub [email protected]:/home/work/gitosis/

在mid机子上对gitosis-admin进行管理,把win主机中的.ssh/id_rsa.pub命名为win.pub并添加到keydir中,并
对gitosis.conf进行配置
[gitosis]

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

[group test]
writable = test
members = mid win


⑧用户mid添加项目test
[mid@MID ~/work]$mkdir test
[mid@MID ~/work]$cd test
[mid@MID ~/work/test]$git init
[mid@MID ~/work/test]$touch test.txt
[mid@MID ~/work/test]$git add test.txt
[mid@MID ~/work/test]$git commit -am "init test project"
[mid@MID ~/work/test]$git remote add origin [email protected]:test.git
[mid@MID ~/work/test]$git push origin master

此时在win主机上测试克隆test项目
win@Debian:~/test$ git clone [email protected]:test.git
Cloning into test...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.


⑨添加已有git项目
如果已经克隆了一个原厂SDK的git仓库,想放到server上供使用。
例如:已经克隆了一个新岸线的7689仓库
[ubuntu@server ~/work]$git clone [email protected]:nu7/tl7689-sdk.git
[ubuntu@server ~/work]$ls
tl7689-sdk
这个时候生成一个裸仓库,远程服务器只要裸仓库就好,即.git目录下的内容
[mid@MID ~/work]$git clone --bare tl7689-sdk tl7689-sdk.git 
[mid@MID ~/work]$ls
tl7689-sdk tl7689-sdk.git

将tl7689-sdk.git仓库移动到git服务器默认仓库目录
git@server:~/repositories$ ls
test.git gitosis-admin.git tl7689-sdk.git


最后回到mid主机,进行gitosis配置
[mid@MID ~/work/testgit/gitosis-admin]$vim gitosis.conf 
[gitosis]

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

[group test]
writable = test
members = mid win

[group tl7689-sdk]
writable = tl7689-sdk
members = mid win

[mid@MID ~/work/testgit/gitosis-admin]$git cm -am "add tl7689-sdk config"
[mid@MID ~/work/testgit/gitosis-admin]$git push origin master


然后在mid克隆server上的tl7689-sdk.git仓库到本地
[mid@MID ~/work/testgit]$git clone [email protected]:tl7689-sdk.git 
正克隆到 'tl7689-sdk'...
remote: Counting objects: 245718, done.
remote: Compressing objects: 100% (176164/176164), done.
remote: Total 245718 (delta 56785), reused 244962 (delta 56037)
接收对象中: 100% (245718/245718), 2.44 GiB | 11.04 MiB/s, done.
处理 delta 中: 100% (56785/56785), done.
检查连接... 完成。
Checking out files: 100% (284468/284468), done.
[mid@MID ~/work/testgit]$ls
foo  foo.git  gitosis-admin   tl7689-sdk
[mid@MID ~/work/testgit]$ls tl7689-sdk/
android  bsp  build  build.sh


管理已有仓库,给远程添加分支
[mid@MID ~/work/testgit/tl7689-sdk]$git br
* master
[mid@MID ~/work/testgit/tl7689-sdk]$
[mid@MID ~/work/testgit/tl7689-sdk]$git br -r
  origin/HEAD -> origin/master
  origin/master
[mid@MID ~/work/testgit/tl7689-sdk]$git br pc205
[mid@MID ~/work/testgit/tl7689-sdk]$git br
* master
  pc205
[mid@MID ~/work/testgit/tl7689-sdk]$git co pc205 
切换到分支 'pc205'
[mid@MID ~/work/testgit/tl7689-sdk]$git br
  master
* pc205
[mid@MID ~/work/testgit/tl7689-sdk]$git push origin pc205:pc205
Total 0 (delta 0), reused 0 (delta 0)
To [email protected]:tl7689-sdk.git
 * [new branch]      pc205 -> pc205
[mid@MID ~/work/testgit/tl7689-sdk]$git br -r
  origin/HEAD -> origin/master
  origin/master
  origin/pc205
[mid@MID ~/work/testgit/tl7689-sdk]$


注:git clone git://xxx,这是一种只读方式来获取代码,无法进行git push。解决方法是用:git clone git@xxx,这是一种可以进行读和代码提交(git push)的方式获取源码,但需要进行密码确认。

你可能感兴趣的:(ubuntu10.04 Git服务器搭建之Gitosis)