Git, 一个分布式的版本管理工具,我认为其革命性的点:在于改变了用户协作的方式,使得协作更简单。

下面讲述 使用一个开源软件 Gitolite搭建一个Git Sever, 并给了一个推荐的团队协助方式。

Install Gitolite

创建 git 用户

创建一个名为 git 用户

[root@server ~]# useradd git

设置密码

[root@server ~]# passwd git


## Download Gitolite ```shell
# 切换为 git 用户
# su git
[git@server ~]$ cd ~
[git@server ~]$ git clone git://github.com/sitaramc/gitolite

Install Gitolite

# 创建 ~/bin 目录
[git@server ~]$ mkdir bin
# 把 /home/git/bin 添加到环境变量里, 通过修改git 家下面的.bashrc
[git@server ~]$ vim .bashrc
# 在文件最后添加
export PATH=/home/git/bin:$PATH
# Install gitolite into $HOME/git/bin
[git@server ~]$ gitolite/install -ln
[git@server ~]$

上传客户端管理员的SSH 公钥 {#ssh_key}

客户端如果生成ssh key, 参考: Github - Generating SSH Keys


[ahnniu@client ~] cd ~/.ssh
[ahnniu@client .ssh] ls -al
# 如果不存在 id_rsa, id_rsa.pub 则运行下面的命令创建
[ahnniu@client .ssh] ssh-keygen -t rsa -C "your_email@example.com"
# 复制一份id_rsa.pub并重命名为 ahnniu.pub, 注 ahnniu为 gitolite管理员的用户名
[ahnniu@client .ssh] cp id_rsa.pub ahnniu.pub
# 通过ssh上传到服务器上(/home/git/),特别注意文件的owern应该为git
[ahnniu@client .ssh] scp ~/.ssh/ahnniu.pub git@192.168.2.223:/home/git/

Setup Gitolite

Refrence: 官方 - setting up gitolite

[git@server ~]$ cd ~
# 基于提供的ahnniu.pub 创建 gitolite-admin 管理仓库
[git@server ~]$ gitolite setup -pk $HOME/ahnniu.pub
Initialized empty Git repository in /home/git/repositories/gitolite-admin.git/
Initialized empty Git repository in /home/git/repositories/testing.git/
WARNING: /home/git/.ssh missing; creating a new one
(this is normal on a brand new install)
WARNING: /home/git/.ssh/authorized_keys missing; creating a new one
(this is normal on a brand new install)

至此,SSH方式的Git服务已经搭建好了

客户端SSH方式clone, push

# 首先需确保,上传的管理员key ahnniu.pub是在这台电脑上生成的,否则是没有权限的
[ahnniu@client ~] git clone git@192.168.2.223:gitolite-admin.git
Cloning into 'gitolite-admin'...
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (6/6), done.
Checking connectivity... done.

gitolite使用

将在最后一节详细介绍。

Gitolite支持HTTP

Install Apache

[root@server ~]# yum install httpd

编辑http.conf,使得apache正常工作

Apache几个重要的路径:

  • /etc/httpd/conf/httpd.conf # apache的主配置
  • /etc/httpd/conf.d/* # 扩展配置
  • /usr/sbin/apachectl # 可执行文件,用于启动,关闭apache服务
  • /var/www/ # apache文档的根目录
  • /var/www/html/ # 网页根目录
  • /var/log/httpd/ # httpd log
[root@server ~]# /etc/httpd/conf/httpd.conf
# 修改下面2个变量
Listen 192.168.2.223:80
ServerName 192.168.2.223:80

添加一个index.html, 看是否可访问

[root@server ~]# cd /var/www/html/
[root@server html]# vim index.html
# 添加以下内容.
Hello World!
# 需把index.html的所有权转为apache用户
[root@server html]# chown apache:apache index.html
# 开启 httpd
# 这里可能会有端口被占用的一些问题,使用命令netstat 查看是哪个命令
[root@server html]# /usr/sbin/apachectl -k start

从客户端访问 http://192.168.2.223/index.html, 如果可能,说明apache安装成功

修改~/.gitolite.rc

[git@server ~]$ cd
# 编辑 ~/.gitolite.rc, 设置PATH变量:(添加gitolite 所在的bin到$PATH)
[git@server ~]$ vim .gitolite.rc
# 添加下面一句道文件最__前面__
$ENV{PATH} .= ":/home/git/bin";

check which document root your Apache's suexec accepts

[root@server ~]# suexec -V
-D AP_DOC_ROOT="/var/www"
-D AP_GID_MIN=100
-D AP_HTTPD_USER="apache"
-D AP_LOG_EXEC="/var/log/httpd/suexec.log"
-D AP_SAFE_PATH="/usr/local/bin:/usr/bin:/bin"
-D AP_UID_MIN=500
-D AP_USERDIR_SUFFIX="public_html"

在AP_DOC_ROOT 目录下创建一个bin, 供git用户执行

[root@server ~]# install -d -m 0755 -o git -g git /var/www/bin

在AP_DOC_ROOT 目录下创建一个git 目录 , 供 apache 用户 做 别名映射

[root@server ~]# install -d -m 0755 -o apache -g apache /var/www/git

在 /var/www/bin 中 添加一个 shell 脚本, 供 git用户调用 gitolite功能 : gitolite-suexec-wrapper.sh

[root@server ~]# cd /var/www/bin
[root@server bin]# vim gitolite-suexec-wrapper.sh
# 内容为
#!/bin/bash
#
# Suexec wrapper for gitolite-shell
# export GIT_PROJECT_ROOT="/home/git/repositories"
export GITOLITE_HTTP_HOME="/home/git" # 为gitolite 下载目录 git clone git://github.com/sitaramc/gitolite
exec ${GITOLITE_HTTP_HOME}/gitolite/src/gitolite-shell
# git为owner
[root@server bin]# chown git:git gitolite-suexec-wrapper.sh
# 修改执行权限
[root@server bin]# chmod 0700 gitolite-suexec-wrapper.sh
# 查看权限有无修改正确
[root@server bin]# ls -al

NOTE: gitolite-suexec-wrapper.sh 文件中不能出现中文,包括空格,否则会出现format错误。

如果遇到问题,可以参考:http://stackoverflow.com/questions/14860166/solved-gitolite-and-http-error-500-permission-issue-in-setup

配置 Apache 使其可与 gitolite 工作

[root@server ~]# cd /etc/httpd/conf.d
# 在 conf.d 添加 gitolite的配置
[root@server conf.d] vim gitolite.conf
# 内容为
<Directory /var/www/git>
Options None
AllowOverride none
Order allow,deny
Allow from all
</Directory> SuexecUserGroup git git
ScriptAlias /git/ /var/www/bin/gitolite-suexec-wrapper.sh/
ScriptAlias /gitmob/ /var/www/bin/gitolite-suexec-wrapper.sh/ <Location /git>
AuthType Basic
AuthName "Git Access"
Require valid-user
AuthUserFile /etc/httpd/git.passwd
</Location>

创建Git Access 认证用户信息

我们在上面的配置:AuthUserFile /etc/httpd/git.passwd

[ahnniu@server ~]  cd /etc/httpd/
# 创建git.passwd, 并创建 ahnniu的用户
[ahnniu@server ~] htpasswd -c git.passwd ahnniu
#接下来输入密码

Finally

  • add an R = daemon access rule to all repositories you want to make available via http.

重启apache , 测试

gitolite log文件:/home/git/.gitolite/logs

[root@server ~]# /usr/sbin/apachectl -k stop
[root@server ~]# /usr/sbin/apachectl -k start
[ahnniu@client ~] git clone http://192.168.2.223/git/testing.git

团队开发过程中建议的Gitolite使用方式

Gitolite的管理方式

我认为Gitolite是典型的吃自己的狗粮,他的管理完全是操作Git来完成, 他可以允许多个管理员同时管理,因为是基于一个git仓库,那么通过merge可以解决冲突的问题

Gitolite有一个gitolite-admin.git的仓库, 通过操作:

  • /keydir/ 来管理SSH用户
  • /conf/gitolite.conf 来管理repo(增删,权限)

下面我们探讨如果通过gitolite打造Github那样的Git Server.

Git仓库的几种权限

Public / Private

Public: 仓库可以给任何人Clone,pull

Private: 仓库只能给指定的人Clone,pull

几种权限组

  1. 权限组: Owner

仓库的拥有者,可以对仓库做任何想做的事情,比如push, 修改其它人访问这个仓库的权限,甚至删除,至少需要有一个人

  1. 权限组: RW

可读写组, clone, push, pull

  1. 权限组: R

可读组, clone, pull

其中 Owner包含 RW, RW权限 包含 R

准备工作

克隆gitolite管理仓库

NOTE: 需使用安装Gitolite指定的SSH Key对应的电脑(或者Copy 对应的 id_rsa到你使用的电脑~/.ssh), HTTP方式好像不能操作gitolite-admin.git仓库。

[ahnniu@client ~] $ git clone git@192.168.2.223:gitlite-admin.git gitolite-admin
[ahnniu@client ~] tree
├── conf
│   └── gitolite.conf
└── keydir
└── ahnniu.pub 2 directories, 2 files

Gitolite的用户管理

Refernce: Gitolite官方 - adding and removing users

用户名

建议的用户名:尽可以由 字母,数字, - , _ 来组成,最少3个字符,不能包括#,$,@等特殊符号

举例: paul

用户名的用途:

SSH用户管理

SSH协议很安全,另外,配置好之后也无需输入密码什么的,也很方便。

假定新增 用户名: paul

Step1: paul 在他本机电脑上:生成SSH Key

参考 文章的上部

Step2: paul 把生成的 paul.pub 发给管理员,由管理员添加

Step3: 管理员在Client端 gitolite-admin仓库,复制 paul.pub 到 ./keydir中

Step4: 管理员commit仓库,并push, gitolite配置生效

[ahnniu@client gitolite-admin] git commit -m "add user: paul"
[ahnniu@client gitolite-admin] git push origin

Step5: 管理员可以 使用 paul 这个用户名 配件权限了

多个SSH Key

一个人经常会有几个办公的场所/电脑,比如公司,家,笔记本,那么如果需要在这些电脑上操作Git的话,都需要配置SSH Key, 并把这些Key发给管理员添加

建议的SSH Public Key的命名为:

  • paul@home.pub # 家里电脑
  • paul@work.pub # 公司电脑

HTTP 用户管理

需管理员登录到服务器操作:

[ahnniu@server ~]  cd /etc/httpd/
# 基于git.passwd文件, 并创建 paul的用户
[ahnniu@server ~] htpasswd git.passwd paul
#接下来输入密码

删除用户

假定删除 paul 用户,gitolite-admin仓库中

  • /conf/gitolite.conf 删除所有跟 paul
  • /keydir/ 删除 paul.pub, paul@*.pub
  • 服务器 /etc/httpd/git.passwd 中删除 paul 用户 (htpasswd -D /etc/httpd/git.passwd paul)

Gitolite的团队管理

一个Team表示一些具备同类属性的人,如果就Git仓库来说,他们操作的是同一些Git仓库。

一个Team也是一个实体,他可以拥有Git仓库。Team的成员可以按照Owern, RW, R三个不同的权限分成3组。

Github的做法:首先是一个Orignization, Orignization下再分多个Team, 每个Team对应一个权限组,或者Owern, 或者RW, 或者R, 那么这个Github下面的Team的共性在于:操作同一些Git仓库,而且具有相同的权限操作。这对大公司而言,比较合适,比如一个公司研发团队很大,又分为了研发一部,研发二部等等。

详细可参考:

对于小公司而言,直接按照我上面提到的应该更合适一些。


在Gitolite中,推荐的团队呈现方式:

# coocox team owner组
@coocox_owner = paul
# coocox team 读写组
@coocox_rw = tom jim
# coocox team 只读组
@coocox_r = alice sky
# 三个小组合并起来,就是coocox这个team
@coocox = @coocox_owner @coocox_rw @coocox_r

Gitolite 仓库管理

新增一个仓库

NOTE: 新建仓库 无需 在Server端 repositories 中 运行 git init --bare, 直接在gitolite.conf中添加下面配置语句,gitolite会帮我们做到

# cox 是要创建的仓库名称, 对应url: git@192.168.2.223: cox.git
repo cox
RW+ = ahnniu
R = @all

我们看到github上的仓库命名:git@github.com:coocox/cox.git, 很清晰,一样可以看到仓库的owner, gitolite是否支持这样的? 答案是支持的。

# cox 是要创建的仓库名称, 对应url: git@192.168.2.223: cox.git
# ahnniu是用户名,用户名是不可能更改的
repo ahnniu/cox
RW+ = ahnniu
R = @all

Refrence: Gitolite官方 - adding and removing repos

删除一个仓库

  1. gitolite.conf 中找到对应的 repo , 移除
  2. 登录到服务器,从 repositories中删除对应的仓库

重命名一个仓库

  1. 登录到服务器,在repositories下手动重命名
  2. gitolite.conf 重命名

管理一个Team的仓库


# 用于归纳
@coocox_repo = coocox/cox coocox/coide repo coocox/cox
RW+ = @coocox_owner @coocox_rw
R = @coocox_r repo coocox/coide
RW+ = @coocox_owner @coocox_rw
# 除coocox team的人可Read外,paul这个个人也是可以读的
R = @coocox_r paul # 不推荐下面 大锅饭的管理方式:
# 这样不允许特例的出现,比如Team下的仓库还希望可以被某个个人访问
repo @coocox_repo
RW+ = @coocox_owner @coocox_rw
R = @coocox_r
#

Gitolite的权限管理

我们用到gitolite仓库的权限主要有两个:

  • RW+,仅需在对应的行 添加一个 team (@coocox_rw) 或者 个人 (paul)
  • R, 同上
  • owner, 不在gitolite仓库权限本身体现,他是gitolite权限之外的,具备管理员的某些特性,比如要删除一个操作,

更改Public / Private

repo ahnniu/cox
RW+ = ahnniu
# R组, 添加@all 就是 Public
# R组, 移除 @all 就是 Private
R = @all

Refrence

# 基于Gitolite搭建Git Server - 支持SSH&HTTP的更多相关文章

  1. 用gitolite搭建git server

    在Ubuntu上测试安装一下git server,为后面项目的代码管理做准备.记录流水账如下, 中间关于git 命令的使用说明不做过多解释,需要了解的请google或者直接git help: 我用到了 ...

  2. Ubuntu server 搭建Git server

    Ubuntu server 搭建Git server,git相比svn,最主要就是分布式了,每个客户端用户的本地都是一个版本管理控制器. Ubuntu server 版本为12.04 搭建步骤如下: ...

  3. Ubuntu server 搭建Git server【转】

    转自:http://www.cnblogs.com/candle806/p/4064610.html Ubuntu server 搭建Git server,git相比svn,最主要就是分布式了,每个客 ...

  4. windows上如何搭建Git Server

    Git在版本控制方面,相比与SVN有更多的灵活性,对于开源的项目,我们可以托管到Github上面,非常方便,但是闭源的项目就会收取昂贵的费用.那么私有项目,如何用Git进行代码版本控制呢?我们可以自己 ...

  5. 搭建Git Server

    windows上如何搭建Git Server   Git在版本控制方面,相比与SVN有更多的灵活性,对于开源的项目,我们可以托管到Github上面,非常方便,但是闭源的项目就会收取昂贵的费用.那么私有 ...

  6. 在Windows上搭建Git Server

    Git在版本控制方面,相比与SVN有更多的灵活性,对于开源的项目,我们可以托管到Github上面,非常方便,但是闭源的项目就会收取昂贵的费用. 那么私有项目,如何用Git进行代码版本控制呢?我们可以自 ...

  7. 在windows上搭建git server Gitblit

    在Windows上搭建Git Server   第1步:下载Java并安装Java.我这里下载的是jdk1.7.0_79 第2步:配置Java环境变量 右键” 计算机” => ”属性” => ...

  8. 使用gitolite搭建Git服务器

    使用gitolite搭建Git服务器 运行环境 Ubuntu18.04 gitolite 搭建过程 安装好Ubuntu18.04系统 更新系统 sudo apt update sudo apt upg ...

  9. 局域网git服务器搭建(基于win7 + bonobo git server)

    公司内网有一台win7系统的服务器. 准备在上面部署git后台, 用于内网项目版本管理. 搜索了相关资料后, 在根据公司环境, 决定采用win7 + bonobo git server + git的方 ...

随机推荐

  1. Unity3d:使用uWebKit插件嵌入网页,网页中的flv视频无法播放

    问题描述:unity3d程序,使用uWebKit插件嵌入网页,用来播放FLV视频,有的电脑可以正常播放,有的电脑在网页中播放不了ps:网页中的播放器用的是player.swf解决方案:是由于网页中的播 ...

  2. HTML5中script的async属性异步加载JS

    HTML5中script的async属性异步加载JS     HTML4.01为script标签定义了5个属性: charset 可选.指定src引入代码的字符集,大多数浏览器忽略该值.defer 可 ...

  3. 高性能的JavaScript -- 读书笔记

    高性能的JavaScript 一.      加载和运行 将脚本放在底部 脚本下载解析执行时,页面已经加载完成并显示在用户面前 成组脚本 减少外部脚本文件数量,整合成一个文件 延迟脚本 动态脚本元素 ...

  4. 【转】Google推荐的命名规则——Android图片资源

    http://blog.csdn.net/yy1300326388/article/details/45443477 1.译 资产类型 前缀 例子 图标 ic_ ic_star.png 启动图标 ic ...

  5. Hadoop on Mac with IntelliJ IDEA - 6 解决KeyValueTextInputFormat读取时只有key值问题

    本文讲述使用KeyValueTextInputFormat在Hadoop 0.x正常工作.Hadoop 1.2.1失效的解决过程. 环境:Mac OS X 10.9.5, IntelliJ IDEA ...

  6. 查看系统和PowerShell版本

    查询PowerShell当前版本$psversiontable.BuildVersion.Major 查询Windows当前版本:[System.Environment]::OSVersion.Ver ...

  7. Swift学习笔记十四

    Deinitialization 当类的实例对象即将要被释放时,会立即调用deinitializer,通过deinit关键字来定义deinitializer,和initializer一样,它也只存在于 ...

  8. cdoj 80 Cube 水题

    Cube Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/80 Descrip ...

  9. UVA 573 (13.08.06)

     The Snail  A snail is at the bottom of a 6-foot well and wants to climb to the top.The snail can cl ...

  10. windows7添�windows2008R2域配置

    server端配置: windows2008R2 WIN+R -> dcmopro -> 下一步 -> .... client配置: windows7 配置 DNS,如图: 计算机 ...