在OS X上搭建Git服务器
我勒了擦,配置个Git服务器居然浪费了哥一天的时间。各种问题不断。
1.准备
在系统设置-用户和组中,添加一个新的普通用户,名称为git。
在共享中开启文件共享
远程登录
网络共享
,设置为允许git访问。
确认python
和git
已经安装了。
2.制作公钥
为了省去来回切换用户的麻烦,首先在客户端制作公钥。
客户端可以是同一台机器的其他用户或另外一台机器。我这里用的当前登录用户xujingbao作为客户端。执行以下命令:
1 | ssh-keygen -t rsa |
提示输入3次,直接打回车就可以了。id_rsa(私钥)和id_rsa.pub(公钥)就生成了,然后把公钥复制到服务器端的/tmp目录下,顺便把名字改了。
1 | cp ~/.ssh/id_rsa.pub /tmp/xujingbao.pub |
3.安装gitosis
切换到服务端,也就是刚才创建的git用户,安装gitosis;
1 2 3 | git clone git://github.com/res0nat0r/gitosis.git cd gitosis sudo python setup.py install |
!这里因为git是一个普通用户,执行sudo时会报is not in the sudoers file. this is incident will be reported
的错误,所以切换到可以执行sudo的用户,为其添加权限。
1 | vi /etc/sudoers |
在root ALL=(ALL)ALL下边添加一行 git ALL=(ALL)ALL
4.初始化gitosis
1 | sudo -H -u git gitosis-init < /tmp/xujingbao.pub |
初始化成功的话,会有以下提示:
1 2 | Initialized empty Git repository in /Users/git/repositories/gitosis-admin.git/ Reinitialized existing Git repository in /Users/git/repositories/gitosis-admin.git/ |
同时在home目录下会出现一个repositories目录,目录下有一个gitosis-admin.git,那么这个git库就是用来管理所有git库的访问权限的。它有gitosis.conf和一个keydir两个目录,gitosis.conf文件就是权限配置的地方,keydir存放的是所有客户端的公钥,公钥名字必须和配置文件中的member名字对应。
其他修改:
1 2 3 4 5 6 7 | //修改post-update的权限 sudo chmod 755 /Users/git/repositories//gitosis-admin.git/hooks/post-update // touch ~/.bashrc echo PATH=/usr/local/bin:/usr/local/git/bin:\$PATH > .bashrc echo export PATH >> .bashrc cat .bashrc |
这样服务端配置就完成了!
5.gitosis-admin
因为我用xujingbao.pub 初始化了这个库,所以我可以在xujingbao这个用户下clone这个库,然后管理所有的访问权限。
1 | git clone git@host:repositories/gitosis-admin.git |
打开gitosis.conf,有如下内容:
1 2 3 4 5 | [gitosis] [group gitosis-admin] //组名 仓库名 members = xujingbao //成员 writable = gitosis-admin //权限 |
如果我们新添加一个项目叫test,他的成员有zhangsan,lisi,wangwu,zhangsan和lisi有写权限,wangwu只读,那就添加如下内容:
1 2 3 4 | [group test] members = zhangsan lisi wangwu writable = zhangsan lisi readable = wangwu |
在本地修改完权限后,需要提交到服务器:
1 | git push
|
在服务端创建库test:
1 2 3 4 | cd repositories mkdir test cd test git init --bare //--bare是必须的 |
回到客户端在需要git管理的项目目录下进行初始化
1 2 3 4 5 | git init git remote add origin git@host:repositories/test.git git add . git commit -m "commit" git push -u origin master |
这样就把本地的文件推上去了。