中文文档地址:https://git-scm.com/book/zh/v1/服务器上的-Git-Gitosis
1、安装 openssh服务器
$ sudo yum -y install openssh openssh-client
2、创建个人公钥和私钥
在默认用户的主目录路径下,运行以下命令,按照提示创建公钥和私钥
$ ssh-keygen -t rsa
默认生成2048位,如果需要提高安全级别,也可以通过下面的命令创建公钥和私钥
$ ssh-keygen -t rsa -b 4096
默认情况下,公钥和私钥会保存在~/.ssh目录下,如下所示:
$ cd ~/.ssh/;ls
id_rsa id_rsa.pub known_hosts
3、安装 git服务器
$ sudo yum -y install git-core
4、配置 git服务器
创建git服务器管理用户
$ sudo useradd -m git
$ sudo passwd git
创建git仓库存储目录
$ sudo mkdir /home/git/repositories
设置git仓库权限
$ sudo chown git:git /home/git/repositories
$ sudo chmod 755 /home/git/repositories
初始化全局设置
$ git config --global user.name "myname"
$ git config --global user.email "myname@server"
5、安装python的setup tool
$ sudo yum -y install python-setuptools
6、获取并安装gitosis
$ cd /tmp
$ git clone https://github.com/res0nat0r/gitosis.git
$ cd gitosis
$ sudo python setup.py install
7、配置gitosis
$ cp ~/.ssh/id_rsa.pub /tmp
$ sudo -H -u git gitosis-init < /tmp/id_rsa.pub
$ sudo chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update
8、管理gitosis配置
$ cd ~
$ git clone git@hostname:用户名/gitosis-admin.git
$ cd gitosis-admin/
各个用户按照前面提到的办法生成各自的ssh公钥文件后,服务器管理员把所有人的 ssh公钥文件都拿来,拷贝到keydir目录下。
修改gitosis.conf文件,如下所示
[gitosis]
[group gitosis-admin]
writable = gitosis-admin
members = a@server1
[group developers]
writable = helloworld
members = a@server1 b@server2
[group test]
readonly = helloworld
members = c@server3
这个配置文件表达了如下含义:gitosis-admin组成员有a,该组对gitosis-admin仓库有读写权限; developers组有a,b两个成员,该组对helloworld仓库有读写权限; test组有c一个成员,对helloworld仓库有只读权限。 当然目前这些配置文件的修改只是在你的本地,你必须推送到gitserver上才能真正生效。 加入新文件、提交并push到git服务器:
$ git add .
$ git commit -am "add helloworld project and users"
$ git remote add origin ssh://git@hostname/helloworld.git
$ git push origin master
9、实例说明
目标:添加用户 john 和仓库 foo 到gitosis,并和管理员miao合作管理
(1)用户john添加并发送id_rsa.pub给 miao
john:~$ ssh-keygen -t rsa
john:~$ cp /home/john/.ssh/id_rsa.pub /tmp
(2)gitosis管理员miao分配john权限
miao:~$ cd ~/projects
#克隆admin代码
git clone git@192.168.1.115:gitosis-admin
cd gitosis-admin
#查看配置文件
cat gitosis.conf
[gitosis]
[group gitosis-admin]
writable = gitosis-admin
members = miao@u32-192-168-1-110
#admin公钥在 keydir 目录下
ls keydir/
miao@u32-192-168-1-110.pub
#将john的公钥复制到keydir目录
cp /tmp/id_rsa.pub keydir/john.pub
#编辑配置文件,添加 john 用户
vi gitosis.conf
[gitosis]
[group gitosis-admin]
writable = gitosis-admin
members = miao@u32-192-168-1-110
[group foo]
writable = foo
members = miao@u32-192-168-1-110 john
#添加到git中
git add .
git commit -am "add member john and project foo"
git push
(3)用户 miao 添加项目foo
miao:~$ cd ~/projects
mkdir foo
cd foo
git init
touch hello.txt
git add hello.txt
git commit -am ‘first commit’
git remote add origin git@192.168.1.115:foo.git
git push origin master
(4)用户 john clone Foo并修改hello.txt
john:~$ git clone git@192.168.1.115:foo.git
cd foo
ls
date > hello.txt
git commit -am ‘add time to hello.txt’ && git push
(5) 用户 miao pull Foo
miao:cd ~/projects/foo
$ vi .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = git@192.168.1.115:foo.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
#推送到代码仓库
git pull