git 服务器搭建,在自己服务器上搭建私有仓库

创建一个简单的私人Git版本控制服务器,首先得有个服务器(屁话)。这种方式适合人比较少的情况,管理不需要很复杂,只要增加几个账号就能搞定。

如下面的情况,有一个服务器,两个客户端。

服务器:Debian7.5 (192.168.10.14)
用  户:git (git-shell)
仓  库:sample.git
地  址:git@192.168.10.14:/home/git/sample.git

客户端:Mac    mymac.pub(mac git公钥)
       win7   mywin.pub(win7 git公钥)

简单步骤:

  • 1、安装git服务,并创建git账号
  • 2、创建示例仓库sample
  • 3、导入客户端公钥
  • 4、客户端连接并提交内容
  • 5、修改Git权限,只使用git-shell命令
    -- OK --

一、安装git

直接通过apt-get安装git。使用这个方式比较简单,当然也可以使用git源代码自己编译最新版本。

# apt-get update
# apt-get install git -y

1.1 创建git用户

创建git账户并登录,直接指定用户目录到/home/git

# useradd git -d /home/git -m -s /bin/bash
# su git

添加用户 user add
-d:指定用户目录
-m:如果目录不存在则创建
-s:可以指定用户使用的命令

二、创建sample库

创建库很简单,注意使用--bare参数。作为代码仓库服务器并不会在上面修改东西,所以只要有相关提交日子就可以。

$ cd ~
$ git init --bare sample.git

注意: 如果创建仓库的用户是Root,一定要把权限移交给git用户

# chown git:git -R sample.git   # 授权用户和用户组给Git

如果没有权限,第一次提交代码时可能会出现insufficient permission需要权限的错误。

git.exe push --progress "origin" master:master

Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 217 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: error: insufficient permission for adding an object to repository database ./objects
remote: fatal: failed to write object
error: unpack failed: unpack-objects abnormal exit
To git@192.168.10.14:/home/git/sample.git
! [remote rejected] master -> master (unpacker error)
error: failed to push some refs to 'git@192.168.10.14:/home/git/sample.git'

三、设置SSH Key

收集客户端公钥,复制到服务器上。 客户端当前账号RSADSA的数字签名文件id_rsa.pub.ssh用户目录中。通常情况下windows在 %userprofile%\.ssh 目录中,Mac在~/.ssh目录中。

mac上可以直接使用下面命令复制粘贴板上,或使用ssh上传

$ pbcopy < ~/.ssh/id_rsa.pub

win7上可以打开Git GUI界面有个帮助->show SSH Key菜单,如果没创建过可以点击创建SSH的签名文件,并复制公钥保存到服务器上。如果使用第三方的SSHKey服务,那可能实际位置会有些差异。

生成本地用户的签名文件,并把客户端上公钥导入到服务器上。如果有多个的话可以放在一个目录中,方便导入。如果是团队人比较多这种方式可能就不太适合了。

如把客户端的key文件mymac.pubmywin.pub导入。

$ ssh-keygen -t rsa -b 4096
$ cat mymac.pub >> ~/.ssh/authorized_keys
$ cat mywin.pub >> ~/.ssh/authorized_keys

如果是root操作的话记得改回所有者何只读,只是为了安全起见

chown git:git authorized_keys
chmod 400 authorized_keys

3.1 客户端获取sample库,并增加文件上传到服务器

在Mac客户端获取。

$ git clone git@192.168.10.14:/home/git/sample.git
Cloning into 'sample'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.

获取成功OK,增加一个文件,并提交到服务器上

$ cd sample
$ cat >> readme.md
first file.
^c
$ git add .
$ git commit -m "first commit"
$ git push

3.2 服务器上查看状态

$ git log
commit 8b070256af351b43a74753f0b05969fcfe9c7310
Author: moguf <moguf_notify@163.com>
Date:   Sun Apr 3 20:49:17 2016 +0800

    first commit

3.3 在Win客户端获取

windows上可以直接使用gitTortoiseGit获取。

git@192.168.10.14:/home/git/sample.git

新建一个文件并提交到服务器上。在服务器上通过git log就能看到第二提交的内容。mac客户机上git pull能看到刚才新加的文件。

这样Git服务器仓库功能基本完成。

四、安全问题

为安全考虑Git账号只允许使用git-shell。在passwd文件中找到git用户,把/bin/bash直接修改成/usr/bin/git-shell 登录root账号,并修改git的用户权限。

$ su
# vim /etc/passwd

这样git用户只能git-shell命令不能登录了。

使用 su git 命令就会出现下面提示,git用户就无法登录到shell,这样就OK了。

# su git
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access.

五、数据仓库迁移

上面的情况是空仓库,如果原来已经有在使用的仓库,想迁移到新仓库。

mac 下切换

先看一下remote的名字。

$ git remote
origin

一般可以看到origin,这个就是原来仓库名称。使用git remote set_url命令更换地址,如新地址是git@192.168.10.14:/home/git/sample.git

$ git pull
$ git remote set-url origin git@192.168.10.14:/home/git/sample.git
$ git push
  • 获取原来最新代码
  • 切换到新地址
  • 提交当前库内容

win7下切换

如果有装‘TortoiseGit’。直接右击当前目录,进入Settings把Remote中地址替换掉。没装就用上Git自带的,使用上面命令替换。

提示:这种方式在空仓库下迁移比较方便,如果新地址有数据那就比较麻烦了。

六、其它相关:从源码安装Git

系统提供的包比较文档,当然可能会发现有些不得不安装最新版本才能解决。反正会有各种原有会须要升级git。

6.1 安装依赖包

$ sudo apt-get install libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev asciidoc xmlto docbook2x

6.2 下载编译

一般的编译安装方法,makemake install

$ wget https://github.com/git/git/archive/v2.9.1.tar.gz
$ tar fvxz v2.9.1.tar.gz
$ cd git-2.9.1
$ make prefix=/usr all doc info
$ su
# make prefix=/usr install install-doc install-html install-info

安装完成后验证一下git版本

# git --version
git version 2.9.1

上面的方法是全局安装的,如果须要更细节的控制安装可以参考git提供的安装帮助原始文档

提示: 最新版本获取在github上 https://github.com/git/git/releases

初始化本地Git配置

设置本地全局用户信息

git config --global user.name abc
git config --global user.email "abc@abc.com"

6.3 磁盘爆了!

上面的依赖包尺寸有些大。虚拟机本来就是用来测试的,分配的磁盘空间比较小。发生了磁盘空间不足问题,100%占用-_-!!有些杯具,这是意外中的意外。可以使用df -hl命令查看剩余空间。

root@sunroom:/home/abc# df -hl
Filesystem              Size  Used Avail Use% Mounted on
rootfs                  2.5G  2.5G     0 100% /
udev                    10M     0   10M   0% /dev
tmpfs                   76M  224K   76M   1% /run
/dev/disk/by-uuid/371b6c92-bdc9-417d-b37f-fae99e6ecce1  2.5G  2.5G     0 100% /
tmpfs                   5.0M     0  5.0M   0% /run/lock
tmpfs                   213M     0  213M   0% /run/shm
/dev/sda6               5.2G  139M  4.8G   3% /home
tmpfs                   213M     0  213M   0% /tmp

简单清理 删除缓存文件

清理旧版本的软件缓存

# apt-get autoclean

清理所有软件缓存:

# apt-get clean

remote: fatal: Out of memory, malloc failed

test@pm:~/moguf$ git clone git@192.168.10.14:/home/git/sample.git  
Cloning into 'a'...  
remote: Counting objects: 256627, done.  
remote: warning: suboptimal pack - out of memory  
remote: fatal: Out of memory, malloc failed (tried to allocate 317810689 bytes)  
error: git upload-pack: git-pack-objects died with error.  
fatal: git upload-pack: aborting due to possible repository corruption on the remote side.  
remote: aborting due to possible repository corruption on the remote side.  
fatal: early EOF  
fatal: index-pack failed

解决方法

home/git/sample.git/config  

[core]  
        repositoryformatversion = 0  
        filemode = true  
        bare = true  
[pack]  
        window = 0

七、相关问题

提示:
可能会出现 bash: vim: command not found 。安装个vim 或使用vi命令都可以。
apt-get install vim -y

更多相关git的问题可以直接到官网查找。有中文版本帮助,内容比较详实。

git 官网:git-scm.com