安装gitosis
首先是获取gitosis(这里假设你已经安装过git):
git clone git://github.com/res0nat0r/gitosis.git
接下来安装gitosis:
python setup.py install
如果出现以下错误:
Traceback (most recent call last): File "setup.py", line 2, in ? from setuptools import setup, find_packages ImportError: No module named setuptools
或者
-bash: python: command not found
那么你还需要安装python-setuptools:
yum install python-setuptools
接下来添加用来管理仓库的用户,用户名任意,我们这里使用git:
useradd git
修改PATH,使git用户可以调用git:
vi /home/git/.bashrc
PATH=/usr/local/bin:/usr/local/bin/git:$PATH
创建key pair(密钥对),并拷贝public key到/tmp下,这样可以确保gitosis-init命令对其有读取权限:
ssh-keygen -t rsa
cp ~/.ssh/id_rsa.pub /tmp/id_rsa.pub
以git用户来执行gitosis-init命令:
sudo -H -u git gitosis-init < /tmp/id_rsa.pub
此时/home/git下增加了两个目录:
gitosis
repositories
其中gitosis是gitosis的根目录,repositories是仓库存放目录。
如果出现以下错误:
if install git from source, otherwise:raise child_exceptionOSError: [Errno 2] No such file or directory
那么做个symlink:
ln -s /usr/bin/git /usr/local/bin/git #安装好git就可先设置好这个软连接
给脚本post-update赋予可执行权限:
sudo chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update
添加新仓库
gitosis的管理是通过git来管理的,clone一下:
git clone git@localhost:gitosis-admin.git #所在目录可以随意,比如:/home/git-access
如果出现以下错误:
Cloning into gitosis-admin...
ssh: connect to host 192.168.1.30 port 22: Connection refused
fatal: The remote end hung up unexpectedly
cd gitosis-admin
ls -l
-rw-r--r-- 1 weizhifeng staff 124 6 14 13:45 gitosis.conf
drwxr-xr-x 3 weizhifeng staff 102 6 14 13:46 keydir
keydir目录用来存放用户的public key(.pub文件),gitosis.conf为配置文件。
看一下配置文件:
cat gitosis.conf
[gitosis]
[group gitosis-admin]
members = Mac
writable = gitosis-admin
其中group代表一个组,writable是仓库名,members是此仓库的成员,可以有多个成员,用空格进行分割。
添加一个新仓库:
[group test]
members = Mac #这个是xxx.pub文件名,例如:marco.pub
writable = test
把更改提交并push到git@localhost:gitosis-admin.git:
git commit -a -m "添加新仓库test"
git push
*仓库名不要使用_下划线
在本地(代码仓库服务器)创建一个仓库,并push到git@localhost:test.git,gitosis会在/home/git/repositories自动创建test.git这个仓库:
mkdir test
cd test
touch README
git init
git remote add origin git@localhost:test.git
git add .
git commit -am '初始化'
git push origin master
此时,添加了权限的用户可以正常克隆代码了,例如:git clone git@192.168.73.128:test.git