转自:http://blog.csdn.net/yuan892173701/article/details/7775002
一、安装配置git
1、安装git和openssh:
howie@ubuntu-12-04:~$ sudo apt-get install git-core openssh-server openssh-client
添加用户git, 该用户将作为所有代码仓库和用户权限的管理者:
howie@ubuntu-12-04:~$ sudo useradd -m git
howie@ubuntu-12-04:~$ sudo passwd git(设置git管理者用户密码)
2、建立git仓库:
howie@ubuntu-12-04:~$ sudo mkdir /home/git/repositories
让除了git以外的用户对此目录无任何权限:
howie@ubuntu-12-04:~$ sudo chown git:git /home/git/repositories
howie@ubuntu-12-04:~$ sudo chmod 755 /home/git/repositories
二、安装配置gitosis
1、初始化一下服务器的git用户,这一步其实是为了安装gitosis做准备。在任何一 台机器上使用git,第一次必须要初始化一下:
howie@ubuntu-12-04:~$ git config –global user.name “yourname”
howie@ubuntu-12-04:~$ git config –global user.email “yourname@server“
例如:
howie@ubuntu-12-04:~$ git config –global user.name “howie”
howie@ubuntu-12-04:~$ git config –global user.email “name@126.com“
2、安装gitosis:
howie@ubuntu-12-04:~$ sudo apt-get install python-setuptools
howie@ubuntu-12-04:~$ cd /tmp
howie@ubuntu-12-04:/tmp$ git clone https://github.com/res0nat0r/gitosis.git
howie@ubuntu-12-04:/tmp$ cd gitosis
howie@ubuntu-12-04:/tmp/gitosis$ sudo python setup.py install
3、生成ssh公钥
howie@ubuntu-12-04:/tmp/gitosis$ ssh-keygen -t rsa
默认回车,保存到当前用户的主目录下
howie@ubuntu-12-04:/tmp/gitosis$ cp /home/howie/.ssh/id_rsa.pub /tmp/id_rsa.pub
howie@ubuntu-12-04:/tmp/gitosis$ sudo chmod a+r /tmp/id_rsa.pub
4、让gitosis运行起来
howie@ubuntu-12-04:/tmp/gitosis$ sudo -H -u git gitosis-init < /tmp/id_rsa.pub
Initialized empty Git repository in /home/repositories/gitosis-admin.git/
Reinitialized existing Git repository in /home/repositories/gitosis-admin.git/
5、添加可执行权限
howie@ubuntu-12-04:/tmp/gitosis$ su
root@ubuntu-12-04:/tmp/gitosis# cd /home/git/repositories/gitosis-admin.git/
root@ubuntu-12-04:/home/git/repositories/gitosis-admin.git# chmod 755 /home/repo/gitosis-admin.git/hooks/post-update
root@ubuntu-12-04:/home/git/repositories/gitosis-admin.git# exit
三、在服务器上新建一个测试项目仓库
1、我们在服务器上新建一个空的项目仓库,叫“trunk”。
切换到git用户:
howie@ubuntu-12-04:/home/git$ su git
$ cd /home/git/repositories
$ mkdir trunk.git
$ cd trunk.git
$ git init - -bare
注:这是在服务器上运行的,初始化一个根级的git仓库
$ exit
四、管理gitosis配置文件
1、clone仓库gitosis-admin.git
howie@ubuntu-12-04:/home/git$ cd ~
howie@ubuntu-12-04:~$ mkdir work
howie@ubuntu-12-04:~/work$ git clone git@<server ip>:gitosis-admin.git
或者howie@ubuntu-12-04:~$ git clone git@<server ip>:/home/git/repositories/gitosis-admin.git
2、添加git服务器的用户,并生成ssh公钥
howie@ubuntu-12-04:~/work$ sudo useradd -m b
howie@ubuntu-12-04:~/work$ sudo passwd b
howie@ubuntu-12-04:~/work$ su b
$ ssh-keygen -t rsa(默认在用户b的主目录下)
$ exit
howie@ubuntu-12-04:~/work$ su
root@ubuntu-12-04:/home/howie/work# cp /home/b/.ssh/id_rsa.pub gitosis-admin/keydir/b@ubuntu-12-04.pub
root@ubuntu-12-04:/home/howie/work#exit
3、修改gitosis.conf文件,配置如下:
[gitosis]
[group gitosis-admin]
members = howie@ubuntu-12-04
writable = gitosis-admin
[group developers]
writable = trunk
members = howie@ubuntu-12-04 b@ubuntu-12-04
4、加入新文件、提交并push到git服务器:
howie@ubuntu-12-04:~/work$ cd gitosis-admin
howie@ubuntu-12-04:~/work/gitosis-admin$ git add .
howie@ubuntu-12-04:~/work/gitosis-admin$ git commit -am “add trunk prj and users”
howie@ubuntu-12-04:~/work/gitosis-admin$ git push origin master
五、初始化测试项目
1、初始化一个版本
howie@ubuntu-12-04:~/work$ mkdir trunk-ori
howie@ubuntu-12-04:~/work$ cd trunk-ori/
howie@ubuntu-12-04:~/work/trunk-ori$ git init
注:这是在用户端的PC上执行的,为的是初始化一个本地的版本库
howie@ubuntu-12-04:~/work/teamwork-ori$ echo “hello world!” > hello
howie@ubuntu-12-04:~/work/teamwork-ori$ git add .
howie@ubuntu-12-04:~/work/teamwork-ori$ git commit -am “initial version”
howie@ubuntu-12-04:~/work/teamwork-ori$ git remote add origin git@<server ip>:trunk.git
或者howie@ubuntu-12-04:~/work/teamwork-ori$ git remote add origin git@<server ip>:/home/git/repositories/trunk.git
howie@ubuntu-12-04:~/work/trunk-ori$ git push origin master
2、clone trunk仓库
howie@ubuntu-12-04:~/work/teamwork-ori$ su b
$ cd /home/b
$ git clone git@<server ip>:trunk.git
$ cd trunk
$ vim hello
$ git add .
$ git commit -am “b add”
$ git push origin master
$ exit