公有的Git服务器有Github和国内的Gitee,当然我们也可以利用自己的旧电脑、树莓派和群晖等计算机硬件,搭建一台 Git 服务器作为私有仓库使用。具体步骤如下:

1、安装Git

  1. # Centos
  2. [mate@localhost ~]$ sudo yum instal git
  3. # Ubuntu/Debian/Raspberry
  4. mate@intel-nuc:~$ sudo apt install git

接下来我们 创建一个git用户组和用户,用来运行git服务:(可以不添加用户)

  1. $ sudo groupadd git
  2. $ sudo useradd git -g git

2、创建证书登录(可以跳过后面使用密码)

收集所有需要登录的用户的公钥,公钥位于id_rsa.pub文件中,把我们的公钥导入到/home/git/.ssh/authorized_keys文件里,一行一个。
如果没有该文件创建它:

  1. $ cd /home/git/
  2. $ mkdir .ssh
  3. $ chmod 755 .ssh
  4. $ touch .ssh/authorized_keys
  5. $ chmod 644 .ssh/authorized_keys

3、初始化(创建)Git裸仓库

首先我们选定一个目录作为Git仓库,假定是/home/gitrepo/learn_git.git,在/home/gitrepo目录下输入命令:

  1. $ cd /home
  2. $ mkdir gitrepo
  3. $ chown git:git gitrepo/
  4. $ cd gitrepo
  5. $ git init --bare learn_git.git
  6. Initialized empty Git repository in /home/gitrepo/learn_git.git/

以上命令Git创建一个裸仓库,即一个不包含当前工作目录的仓库(服务器上不需要工作空间,只要备份即可)。服务器上的Git仓库通常都以.git结尾。
然后,把仓库所属用户和组都改为git(如果没有创建用户git用户就不用进行这一步)

  1. $ chown -R git:git learn_git.git

4、克隆仓库(本地机)

  1. $ git clone git@192.168.45.4:/home/gitrepo/learn_git.git
  2. Cloning into 'learn_git'...
  3. warning: You appear to have cloned an empty repository.
  4. Checking connectivity...
  5. done.

说明

  1. git@10.168.1.20:/home/gitrepo/learn_git.git
  2. # git 是服务器上的用户名,根据创建库时的用户而定;
  3. # 10.168.1.20 是Git 所在服务器 ip ,你需要将其修改为你自己的 Git 服务 ip。
  4. # /home/gitrepo/learn_git.git 是服务器上的库的绝对路径

Git 服务器安装就完成。