gitosis已经不更新,所以这里用gitolite进行git服务器的搭建。
环境:
Ubuntu16.04 +gitolite
一、安装Ubuntu16.04
在这里,我使用在虚拟机上进行操作。
网上很多教程,跳过这一步
二、安装git、sshopen-server、sshopen-client(服务器端)
ljaer@ubuntu:~$ sudo apt-get update
ljaer@ubuntu:~$ sudo apt-get install git openssh-server openssh-client
三、生成SSH key(客户端)
这里,我的客户端和服务器端在同一台机子
ljaer@ubuntu:~$ ssh-keygen -t rsa -C zk
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ljaer/.ssh/id_rsa):
Created directory '/home/ljaer/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/ljaer/.ssh/id_rsa.
Your public key has been saved in /home/ljaer/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:RiIjGgr6n9xvUBCDQ5hlh6oNK8IxsiaIKwLE9TfkZWY zk
The key's randomart image is:
+---[RSA 2048]----+
| =+o+. |
| oo+..o E |
|+ o.+.+.* |
|*B.. + *. |
|X*o ..S |
|@+. .. |
|*.. . |
|+ o o . |
|o + .o. |
+----[SHA256]-----+
ljaer@ubuntu:~$
运行命令后,一直按回车键知道SSH Key生成
ljaer@ubuntu:~$ cd /home/ljaer/.ssh/
ljaer@ubuntu:~/.ssh$ ls
id_rsa id_rsa.pub
四、将生成的SSH public key拷贝到服务器(客户端)
这里注意,为了区分SSH public key,请将SSH public key重命名,最好是用用户名命名。例如 <user-name>.pub
ljaer@ubuntu:~$ mv /home/ljaer/.ssh/id_rsa.pub zk.pub
ljaer@ubuntu:~$ scp zk.pub ljaer@192.168.3.218:/tmp
The authenticity of host '192.168.3.218 (192.168.3.218)' can't be established.
ECDSA key fingerprint is SHA256:oemRCxCBXtX8OWfvuNo7U4SVKX9KLBMZvYeuYvGMN5w.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.3.218' (ECDSA) to the list of known hosts.
ljaer@192.168.3.218's password:
zk.pub 100% 384 0.4KB/s 00:00
五、创建Git用户(服务器端)
ljaer@ubuntu:~$ sudo adduser \
> --system \
> --shell /bin/bash \
> --gecos 'Git SCM User' \
> --group \
> --disabled-password \
> --home /home/git git
[sudo] password for ljaer:
Adding system user `git' (UID 122) ...
Adding new group `git' (GID 129) ...
Adding new user `git' (UID 122) with group `git' ...
Creating home directory `/home/git' ...
六、下载Gitolite源码(服务器端)
ljaer@ubuntu:~$ sudo su git
git@ubuntu:/home/ljaer$ cd $HOME
git@ubuntu:~$ git clone http://github.com/sitaramc/gitolite
Cloning into 'gitolite'...
remote: Counting objects: 9300, done.
remote: Total 9300 (delta 0), reused 0 (delta 0), pack-reused 9300
Receiving objects: 100% (9300/9300), 2.92 MiB | 134.00 KiB/s, done.
Resolving deltas: 100% (5752/5752), done.
Checking connectivity... done.
七、安装Gitolite(服务器端)
git@ubuntu:~$ mkdir -p ${HOME}/bin
git@ubuntu:~$ ${HOME}/gitolite/install -to ${HOME}/bin
八、设置SSH public key(服务器端)
git@ubuntu:~$ ${HOME}/bin/gitolite setup -pk /tmp/zk.pub
Initialized empty Git repository in /home/git/repositories/gitolite-admin.
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)
九、克隆Gitolite管理库(客户端)
ljaer@ubuntu:~$ git clone git@192.168.3.218:gitolite-admin.git
Cloning into 'gitolite-admin'...
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (4/4), done.
Receiving objects: 100% (6/6), 719 bytes | 0 bytes/s, done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Checking connectivity... done.
你可以看到在管理库里,有两个目录, conf/和keydir/,其中conf/下面有个名为gitolite.conf的配置文件。
conf/gitolite.conf 用于Git项目配置,访问权限设置。
keydir/ 用于存储用户的SSH public key(公钥)。
Git项目配置和访问权限设置请看Gitolite的README.txt文档。
十、配置git项目(客户端)
为了增加Git项目到Gitolite服务器,有两种方法:
方法1:
添加一个新的Git项目
修改gitolite,conf
例如,新建一个名为foo的Git项目,首先需要在conf/gitolite.conf里注册foo项目
假设你已经克隆了gitolite-admin.git项目,编辑conf/gitolite.conf,增加下面的内容
repo foo
RW+ = @all
然后提交这部分修改,并push到远程
ljaer@ubuntu:~/gitolite-admin$ git add .
ljaer@ubuntu:~/gitolite-admin$ git commit -m "add foo" [master e0c537e] add foo
1 file changed, 3 insertions(+)
ljaer@ubuntu:~/gitolite-admin$ git push origin master
Counting objects: 4, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 365 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
remote: Initialized empty Git repository in /home/git/repositories/foo.git/
To git@192.168.3.218:gitolite-admin.git
c72e3b3..e0c537e master -> master
现在Git项目foo已经设置好了。用户可以从服务器上克隆foo了
ljaer@ubuntu:~/gitolite-admin$ git clone git@192.168.3.218:foo.git
Cloning into 'foo'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.
方法2:
添加一个存在的Git项目
如果你有一个已经存在的项目,也想将其添加到Gitolite的服务器里。这很简单,但是首先你依然需要在gitolite.conf里注册这个项目。然后执行下面的操作:
cd foo
git remote rm origin
git remote add origin ljaer@192.168.3.218.foo.git
git push origin master
如果你有多个branch,那么checkout到哪个branch,执行
git push origin <branch-name>
十一、其他客户机下载项目
将其他客户机下载git服务器上的项目
jaer@DESKTOP-OIJKKL8 MINGW64 ~/Desktop
$ git clone git@192.168.3.218:foo.git
Cloning into 'foo'...
git@192.168.3.218's password:
fatal: 'foo.git' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
报错了,项目下载不下来
权限问题,该客户机的id_rsa.pub没有上传到git服务器
为了便于识别,将id_rsa.pub改为zhangkai.pub
上传至gitolite-admin.git项目下的/keydir/目录下
由于刚才第九步是克隆git管理库在192.168.3.218上,所以讲该客户端的git认证上传到该管理库
客户端
jaer@DESKTOP-OIJKKL8 MINGW64 ~/.ssh
$ scp zhangkai.pub ljaer@192.168.3.218:/home/ljaer/gitolite-admin/keydir/
ljaer@192.168.3.218's password:
zhangkai.pub 100% 402 0.4KB/s 00:00
管理端
ljaer@ubuntu:~/gitolite-admin/keydir$ ls
zhangkai.pub zk.pub
ljaer@ubuntu:~/gitolite-admin/keydir$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
zhangkai.pub
nothing added to commit but untracked files present (use "git add" to track)
ljaer@ubuntu:~/gitolite-admin/keydir$ git add .
ljaer@ubuntu:~/gitolite-admin/keydir$ git commit -m "add zhangkai.pub"
[master 7136ed0] add zhangkai.pub
1 file changed, 1 insertion(+)
create mode 100644 keydir/zhangkai.pub
ljaer@ubuntu:~/gitolite-admin/keydir$ git push origin master
Counting objects: 4, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 678 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To git@192.168.3.218:gitolite-admin.git
279ecec..7136ed0 master -> master
再在客户端下载项目
jaer@DESKTOP-OIJKKL8 MINGW64 ~/Desktop
$ git clone git@192.168.3.218:foo.git
Cloning into 'foo'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
Checking connectivity... done.
成功下载。