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

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

Install Gitolite

创建 git 用户

创建一个名为 git 用户

[root@server ~]# useradd git

设置密码

[root@server ~]# passwd git


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

Install Gitolite

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

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

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


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

Setup Gitolite

Refrence: 官方 - setting up gitolite

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

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

客户端SSH方式clone, push

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

gitolite使用

将在最后一节详细介绍。

Gitolite支持HTTP

Install Apache

  1. [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
  1. [root@server ~]# /etc/httpd/conf/httpd.conf
  2. # 修改下面2个变量
  3. Listen 192.168.2.223:80
  4. ServerName 192.168.2.223:80

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

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

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

修改~/.gitolite.rc

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

check which document root your Apache's suexec accepts

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

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

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

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

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

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

  1. [root@server ~]# cd /var/www/bin
  2. [root@server bin]# vim gitolite-suexec-wrapper.sh
  3. # 内容为
  4. #!/bin/bash
  5. #
  6. # Suexec wrapper for gitolite-shell
  7. #
  8. export GIT_PROJECT_ROOT="/home/git/repositories"
  9. export GITOLITE_HTTP_HOME="/home/git"
  10. # 为gitolite 下载目录 git clone git://github.com/sitaramc/gitolite
  11. exec ${GITOLITE_HTTP_HOME}/gitolite/src/gitolite-shell
  12. # git为owner
  13. [root@server bin]# chown git:git gitolite-suexec-wrapper.sh
  14. # 修改执行权限
  15. [root@server bin]# chmod 0700 gitolite-suexec-wrapper.sh
  16. # 查看权限有无修改正确
  17. [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 工作

  1. [root@server ~]# cd /etc/httpd/conf.d
  2. # 在 conf.d 添加 gitolite的配置
  3. [root@server conf.d] vim gitolite.conf
  4. # 内容为
  5. <Directory /var/www/git>
  6. Options None
  7. AllowOverride none
  8. Order allow,deny
  9. Allow from all
  10. </Directory>
  11. SuexecUserGroup git git
  12. ScriptAlias /git/ /var/www/bin/gitolite-suexec-wrapper.sh/
  13. ScriptAlias /gitmob/ /var/www/bin/gitolite-suexec-wrapper.sh/
  14. <Location /git>
  15. AuthType Basic
  16. AuthName "Git Access"
  17. Require valid-user
  18. AuthUserFile /etc/httpd/git.passwd
  19. </Location>

创建Git Access 认证用户信息

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

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

Finally

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

重启apache , 测试

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

  1. [root@server ~]# /usr/sbin/apachectl -k stop
  2. [root@server ~]# /usr/sbin/apachectl -k start
  1. [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仓库。

  1. [ahnniu@client ~] $ git clone git@192.168.2.223:gitlite-admin.git gitolite-admin
  2. [ahnniu@client ~] tree
  3. ├── conf
  4.    └── gitolite.conf
  5. └── keydir
  6. └── ahnniu.pub
  7. 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配置生效

  1. [ahnniu@client gitolite-admin] git commit -m "add user: paul"
  2. [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 用户管理

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

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

删除用户

假定删除 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中,推荐的团队呈现方式:

  1. # coocox team owner组
  2. @coocox_owner = paul
  3. # coocox team 读写组
  4. @coocox_rw = tom jim
  5. # coocox team 只读组
  6. @coocox_r = alice sky
  7. # 三个小组合并起来,就是coocox这个team
  8. @coocox = @coocox_owner @coocox_rw @coocox_r

Gitolite 仓库管理

新增一个仓库

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

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

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

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

Refrence: Gitolite官方 - adding and removing repos

删除一个仓库

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

重命名一个仓库

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

管理一个Team的仓库


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

Gitolite的权限管理

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

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

更改Public / Private

  1. repo ahnniu/cox
  2. RW+ = ahnniu
  3. # R组, 添加@all 就是 Public
  4. # R组, 移除 @all 就是 Private
  5. 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. OC:习题来自平时搜索

    == 第一部分 ==  类变量的@protected ,@private,@public,@package,声明各有什么含义?写一个标准宏MIN,这个宏输入两个参数并返回较小的一个?面向对象的三大特征 ...

  2. C# Hashtable 简述

    一,哈希表(Hashtable)简述 在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似keyvalue的键值对,其中 ...

  3. 微软自带报表rdlc操作(合并同数据项)

    一.使用table合并数据项(隐藏相同数据项). 1.添加table,添加各数据项目和表头. 2.添加分组,选中table1右键属性->组->添加. 3.右键需要隐藏数据项的列->属 ...

  4. 5分钟内使用React、Webpack与ES6构建应用

    http://blog.leapoahead.com/2015/09/12/react-es6-webpack-in-5-minutes/

  5. Microchip 125 kHz RFID System Design Guide

    Passive RFID Basics - AN680 INTRODUCTION Radio Frequency Identification (RFID) systems use radio fre ...

  6. js查看浏览器类型和版本

    var Sys = {}; var ua = navigator.userAgent.toLowerCase(); var s; var scan; (s = ua.match(/msie ([\d. ...

  7. pad 横屏 cell不正常显示

    在iOS9中,适配iPad横屏的时候,我发现cell不能正常显示,其标题和线都不是从左边头部开始,而是在中间,accessoryType的图标也不再右边尾部,效果如下图 但是在iPhone中是正常的, ...

  8. windows无法搜索新更新 80072ee2

      http://windows.microsoft.com/zh-cn/windows/windows-update-error-80072ee2#1TC=windows-7    

  9. 关于c#中的Timer控件的简单用法

    Timer控件主要会用到2个属性一个是Enabled和IntervalEnabled主要是控制当前Timer控件是否可用timer1.Enabled=false;不可用timer1.Enabled=t ...

  10. HDU 5597 GTW likes function 打表

    GTW likes function 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5596 Description Now you are give ...