27

I set up .git in a directory on my local machine. I then run:

mkdir a
cd a
git init
git daemon

When I attempt to clone the repository in a, I get the following error:

mkdir b
cd b
git clone git://127.0.0.1
Initialized empty Git repository in /b/127.0.0.1/.git/
fatal: The remote end hung up unexpectedly

How can I clone my repository over the git protocol?

Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
yazz.com
  • 57,320
  • 66
  • 234
  • 385

2 Answers2

48

You need to let git-daemon know it may export your repository:

$ git init --bare /tmp/my-repo.git
Initialized empty Git repository in /tmp/my-repo.git/

$ git daemon --verbose --base-path=/tmp --export-all /tmp/my-repo.git &

$ git clone git://`hostname`/my-repo.git
Initialized empty Git repository in /tmp/my-repo/.git/
warning: You appear to have cloned an empty repository.

A far better way is to run it from xinetd. Create and tweak /etc/xinetd.d/git along the lines of

# description: The git server offers access to git repositories
service git
{
        disable = no
        type            = UNLISTED
        port            = 9418
        socket_type     = stream
        wait            = no
        user            = nobody
        server          = /usr/local/bin/git
        server_args     = daemon --inetd --export-all --base-path=/pub/scm
        log_on_failure  += USERID
}

Don't forget to sudo killall -HUP xinetd. Now, all git repositories beneath /pub/scm will be available to anyone who asks.

Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
  • Thank you for this answer. Just to add that I found that if you want to be able to `git push` to the remote without using SSH you need to start the git daemon with the flag `--enable=receive-pack`. This allows [anonymous push](https://git-scm.com/docs/git-daemon#Documentation/git-daemon.txt-receive-pack). – peterevans May 22 '20 at 08:35
14

You either have to put an empty file called git-daemon-export-ok into the repository or start git daemon with the --export-all option.

Quote from the git-daemon man page:

It verifies that the directory has the magic file "git-daemon-export-ok", and it will refuse to export any git directory that hasn't explicitly been marked for export this way (unless the --export-all parameter is specified). If you pass some directory paths as git daemon arguments, you can further restrict the offers to a whitelist comprising of those.

Fabian Jakobs
  • 28,815
  • 8
  • 42
  • 39
  • 1
    Just adding this: In my case "git-daemon-export-ok" wasn't readable by the git-ro user that was running the daemon. – Belac Feb 07 '14 at 14:22