Home | 简体中文 | 繁体中文 | 杂文 | 打赏(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎专栏 | Search | Email

181.5. git-daemon 服务器

181.5.1. git-daemon - A really simple server for git repositories

在/home/gitroot/ 上运行 git 守护进程

$ cd /home/gitroot
$ mkdir test.git
$ cd test.git
$ git --bare init --shared
Initialized empty shared Git repository in /home/gitroot/test.git/
git daemon --verbose --export-all --base-path=/home/gitroot --enable=receive-pack --reuseaddr


sudo git daemon --verbose --export-all --base-path=/home/gitroot --enable=upload-pack --enable=upload-archive --enable=receive-pack


$ git config daemon.receivepack true
$ git config --file config receive.denyCurrentBranch ignore

181.5.2. git-daemon-sysvinit

for a read-only repo:

$ sudo apt-get install git-daemon-sysvinit

$ dpkg -l git-daemon-sysvinit
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                                   Version                  Architecture             Description
ii  git-daemon-sysvinit                    1:      all                      fast, scalable, distributed revision control system (git-daemon service)

$ dpkg -L git-daemon-sysvinit

配置 /etc/default/git-daemon 文件

181.5.3. inet.conf / xinetd 方式启动

过程 181.2. git-daemon

  1. /etc/shells

    /etc/shells 最后一行添加 '/usr/bin/git-shell'

    $ grep git /etc/shells
  2. add new user 'git' and 'gitroot' for git

    you need to assign shell with /usr/bin/git-shell

    $ sudo adduser git --shell /usr/bin/git-shell
    $ sudo adduser gitroot --ingroup git --shell /bin/bash


    $ grep git /etc/passwd
  3. /etc/services

    $ grep 9418 /etc/services
    git             9418/tcp                        # Git Version Control System
  4. /etc/inet.conf

    $ grep git /etc/inet.conf
    git     stream  tcp     nowait  nobody \
      /usr/bin/git-daemon git-daemon --inetd --syslog --export-all /home/gitroot

    reload inetd

    $ sudo pkill -HUP inetd
  5. xinetd

    目前的Linux逐渐使用xinetd.d替代inet.conf,如Redhat系列已经不再使用inet.conf, Ubuntu系列发行版已经不预装inet与xinetd

    $ apt-cache search xinetd
    globus-gfork-progs - Globus Toolkit - GFork Programs
    rlinetd - gruesomely over-featured inetd replacement
    update-inetd - inetd configuration file updater
    xinetd - replacement for inetd with many enhancements
    $ sudo apt-get install xinetd


    $ cat /etc/xinetd.d/git
    # default: off
    # description: The git server offers access to git repositories
    service git
            disable 		= no
            type            = UNLISTED
            port            = 9418
            socket_type     = stream
            protocol 		= tcp
            wait            = no
            user            = gitroot
            server          = /usr/bin/git
            server_args     = daemon --inetd --export-all --enable=receive-pack --reuseaddr --base-path=/home/gitroot
            log_on_failure  += USERID

    reload xinitd

    $ sudo /etc/init.d/xinetd reload
     * Reloading internet superserver configuration xinetd                                       [ OK ]

181.5.4. git-daemon-run

$ sudo apt-get install git-daemon-run


$ cat /etc/passwd | grep git

git-daemon-run 包携带的文件

$ dpkg -L git-daemon-run


$ find /etc/sv/git-daemon/


$ sudo vim /etc/sv/git-daemon/run

exec 2>&1
echo 'git-daemon starting.'
exec chpst -ugitdaemon \
  "$(git --exec-path)"/git-daemon --verbose --reuseaddr \
    --base-path=/var/cache /var/cache/git

git-daemon --verbose --reuseaddr \
    --base-path=/var/cache /var/cache/git


git-daemon --verbose --reuseaddr \
	--enable=receive-pack --export-all --base-path=/opt/git

* 我加上了一个--export-all 使用该选项后,在git仓库中就不必创建git-daemon-export-ok文件。

其他选项--enable=upload-pack --enable=upload-archive --enable=receive-pack

/etc/services 文件中加入

# Local services
git             9418/tcp                        # Git Version Control System


$ grep 9418 /etc/services


$ sudo sv stop git-daemon


$ sudo runsv git-daemon
runsv git-daemon: fatal: unable to change to directory: file does not exist


$ nmap localhost

Starting Nmap 5.00 ( http://nmap.org ) at 2012-01-31 10:45 CST
Warning: Hostname localhost resolves to 2 IPs. Using
Interesting ports on localhost (
Not shown: 989 closed ports
21/tcp   open  ftp
22/tcp   open  ssh
53/tcp   open  domain
80/tcp   open  http
111/tcp  open  rpcbind
139/tcp  open  netbios-ssn
445/tcp  open  microsoft-ds
1723/tcp open  pptp
3128/tcp open  squid-http
3306/tcp open  mysql
9418/tcp open  git

181.5.5. Testing

$ sudo mkdir -p /opt/git/example.git
$ cd /opt/git/example.git
$ git init
$ sudo vim example.git/.git/config
denyCurrentBranch = ignore

$ sudo chown gitdaemon -R /opt/git/*
$ touch git-daemon-export-ok

.git/config 文件应该是下面这样

$ cat example.git/.git/config
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true

denyCurrentBranch = ignore

git-clone git://localhost/example.git

neo@deployment:/tmp$ git clone git://localhost/example.git example.git
Cloning into example.git...
warning: You appear to have cloned an empty repository.
neo@deployment:/tmp$ cd example.git/
neo@deployment:/tmp/example.git$ echo helloworld > hello.txt
neo@deployment:/tmp/example.git$ git add hello.txt
neo@deployment:/tmp/example.git$ git commit -m 'Initial commit'
[master (root-commit) 65a0f83] Initial commit
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 hello.txt

我们添加了一些文件 push 到服务器

$ git push origin master
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 214 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git://localhost/example.git
 * [new branch]      master -> master

然后再git clone,可以看到文件数目

$ git-clone git://localhost/example.git
Cloning into example...
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.