In the section on remote repositories , we said that remote repositories are actually no different from local repositories, purely for 7x24 hours of booting and exchanging your changes.
GitHub is a remote repository for hosting open source code for free. However, for some commercial companies that regard source code as life, they neither want to disclose the source code, nor are they willing to pay GitHub protection fees, so they can only build a Git server by themselves as a private repository.
Setting up a Git server requires preparing a machine running Linux, Ubuntu or Debian is highly recommended, so that the apt
installation can be completed with a few simple commands.
Assuming you already have a sudo
privileged user account, let's start the installation.
The first step, install git
:
$ sudo apt-get install git
The second step is to create a git
user to run the git
service:
$ sudo adduser git
The third step, create a certificate login:
Collect the public keys of all users who need to log in, which is their own id_rsa.pub
file, and import all the public keys into the /home/git/.ssh/authorized_keys
file, one per line.
The fourth step is to initialize the Git repository:
First select a directory as the Git repository, assuming it is /srv/sample.git
, /srv
enter the command in the directory:
$ sudo git init --bare sample.git
Git will create a bare repository. The bare repository has no workspace. Because the Git repository on the server is purely for sharing, users are not allowed to log in directly to the server to change the workspace, and the Git repository on the server usually .git
ends with . Then, change owner to git
:
$ sudo chown -R git:git sample.git
The fifth step, disable shell login:
For security reasons, the git user created in the second step is not allowed to log in to the shell, which can be done by editing the /etc/passwd
file. Find a line similar to the following:
git:x:1001:1001:,,,:/home/git:/bin/bash
Change it to:
git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell
这样,git
用户可以正常通过ssh使用git,但无法登录shell,因为我们为git
用户指定的git-shell
每次一登录就自动退出。
第六步,克隆远程仓库:
现在,可以通过git clone
命令克隆远程仓库了,在各自的电脑上运行:
$ git clone git@server:/srv/sample.git
Cloning into 'sample'...
warning: You appear to have cloned an empty repository.
剩下的推送就简单了。
管理公钥
如果团队很小,把每个人的公钥收集起来放到服务器的/home/git/.ssh/authorized_keys
文件里就是可行的。如果团队有几百号人,就没法这么玩了,这时,可以用Gitosis来管理公钥。
这里我们不介绍怎么玩Gitosis了,几百号人的团队基本都在500强了,相信找个高水平的Linux管理员问题不大。
在远程仓库一节中,我们讲了远程仓库实际上和本地仓库没啥不同,纯粹为了7x24小时开机并交换大家的修改。
GitHub就是一个免费托管开源代码的远程仓库。但是对于某些视源代码如生命的商业公司来说,既不想公开源代码,又舍不得给GitHub交保护费,那就只能自己搭建一台Git服务器作为私有仓库使用。
搭建Git服务器需要准备一台运行Linux的机器,强烈推荐用Ubuntu或Debian,这样,通过几条简单的apt
命令就可以完成安装。
假设你已经有sudo
权限的用户账号,下面,正式开始安装。
第一步,安装git
:
$ sudo apt-get install git
第二步,创建一个git
用户,用来运行git
服务:
$ sudo adduser git
第三步,创建证书登录:
收集所有需要登录的用户的公钥,就是他们自己的id_rsa.pub
文件,把所有公钥导入到/home/git/.ssh/authorized_keys
文件里,一行一个。
第四步,初始化Git仓库:
先选定一个目录作为Git仓库,假定是/srv/sample.git
,在/srv
目录下输入命令:
$ sudo git init --bare sample.git
Git就会创建一个裸仓库,裸仓库没有工作区,因为服务器上的Git仓库纯粹是为了共享,所以不让用户直接登录到服务器上去改工作区,并且服务器上的Git仓库通常都以.git
结尾。然后,把owner改为git
:
$ sudo chown -R git:git sample.git
第五步,禁用shell登录:
出于安全考虑,第二步创建的git用户不允许登录shell,这可以通过编辑/etc/passwd
文件完成。找到类似下面的一行:
git:x:1001:1001:,,,:/home/git:/bin/bash
改为:
git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell
这样,git
用户可以正常通过ssh使用git,但无法登录shell,因为我们为git
用户指定的git-shell
每次一登录就自动退出。
第六步,克隆远程仓库:
现在,可以通过git clone
命令克隆远程仓库了,在各自的电脑上运行:
$ git clone git@server:/srv/sample.git
Cloning into 'sample'...
warning: You appear to have cloned an empty repository.
剩下的推送就简单了。
管理公钥
如果团队很小,把每个人的公钥收集起来放到服务器的/home/git/.ssh/authorized_keys
文件里就是可行的。如果团队有几百号人,就没法这么玩了,这时,可以用Gitosis来管理公钥。
这里我们不介绍怎么玩Gitosis了,几百号人的团队基本都在500强了,相信找个高水平的Linux管理员问题不大。
管理权限
有很多不但视源代码如生命,而且视员工为窃贼的公司,会在版本控制系统里设置一套完善的权限控制,每个人是否有读写权限会精确到每个分支甚至每个目录下。因为Git是为Linux源代码托管而开发的,所以Git也继承了开源社区的精神,不支持权限控制。不过,因为Git支持钩子(hook),所以,可以在服务器端编写一系列脚本来控制提交等操作,达到权限控制的目的。Gitolite就是这个工具。
这里我们也不介绍Gitolite了,不要把有限的生命浪费到权限斗争中。