Torsten Curdt’s weblog

From subversion to git (part1 – git)

You read it everywhere – obviously git must be the best since sliced bread. But replacing subversion with git is not exactly just as easy as one would hope. Installing git itself is simple. On Ubuntu just have to install the “git-core” package. But I would recommend some other packages as well.

apt-get install git-core git-svn gitweb

Done. You can already use git and push a git repository onto the server via ssh protocol. You would only need to expose the directory via http. If you want shared repository with permission administration you will need a slightly more complex setup. At least if you don’t want to hand out ssh account for every person. So I’d suggest you create your repository directory in the home directory of a separate “git” user

sudo adduser \
    --system \
    --shell /bin/sh \
    --gecos 'git version control' \
    --group \
    --disabled-password \
    --home /home/git \
sudo -u git mkdir /home/git/repositories

Copy your public ssh key onto the machine as “/home/git/.ssh/authorized_keys” and you should be able to log into the machine with “ssh git@yourserver.com”.

In order to make the repositories available we now start the git daemon.

sudo -u git git-daemon
  --reuseaddr \
  --verbose \
  --base-path=/home/git/repositories/ \

There is also a package called “git-daemon-run” to start the daemon on system start. Frankly speaking I found it way too complicated and just created a simple init.d script that does the trick.

sudo cat > /etc/init.d/git-daemon << EOF

test -f /usr/bin/git-daemon || exit 0

. /lib/lsb/init-functions

GITDAEMON_OPTIONS="--reuseaddr --verbose --base-path=/home/git/repositories/ --detach"

case "$1" in
start)  log_daemon_msg "Starting git-daemon"

        start-stop-daemon --start -c git:git --quiet --background \
                     --exec /usr/bin/git-daemon -- ${GITDAEMON_OPTIONS}

        log_end_msg $?
stop)   log_daemon_msg "Stopping git-daemon"

        start-stop-daemon --stop --quiet --name git-daemon

        log_end_msg $?
*)      log_action_msg "Usage: /etc/init.d/git-daemon {start|stop}"
        exit 2
exit 0
update-rc.d git-daemon defaults

Once started you can create a new project

cd testproject
git init
git add .
git commit -m "initial import"
git remote add origin git@yourserver.com:repositories/testproject.git
touch .git/git-daemon-export-ok
scp -rp .git git@yourserver.com:repositories/testproject.git
git push --all

which pushes the initial project to the git server via ssh protocol. The “git-daemon-export-ok” makes it accessible to the outside world via git daemon. Now everyone can clone the repository which is the initial checkout and the first step to follow your work.

git clone git://yourserver.com/testproject.git

Now let’s also expose the git repository over http similar to viewvc

  • man scp

    Option "-P" let's you specify the port
  • absuk

    when i put scp -rp .git git@yourserver.com:repositories/testproject.git
    ssh: connect to host yourserver.com port 22: Connection refused
    lost connection.

    I have my SSH on a diffrent port. howcan i make this work on my port, i have put a .ssh/config file and changed the port there. but that still did not work.

    any ideas.. cheers for the tutorial
  • IIRC you have to create the bare repo on the server first. Let me know if that works for you then I'll update the blog post.
  • Evert
    in the last steps everytinhg works except
    "git push --all" it gives this error

    MacBook:testproject evertmacbook$ git push --all
    ssh: Could not resolve hostname git: nodename nor servname provided, or not known
    fatal: The remote end hung up unexpectedly

    what did i do wrong?

    THNX !
  • Testd
  • Stephane
    On my current debian testing (may 2010), git-daemon doesn't exist. I have to execute 'git daemon' (so without the hyphen). I ligthly modified your script:

    - test -f /usr/bin/git-daemon || exit 0
    +test -f /usr/bin/git || (echo "git not found!"; exit 0)

    - --exec /usr/bin/git-daemon -- ${GITDAEMON_OPTIONS}
    + --exec /usr/bin/git daemon -- ${GITDAEMON_OPTIONS}

    Note for users (interested by my change or not): don't copy the code source displayed in your browser but from the html source because some dollar are not displayed.
  • Pontus
    On Ubuntu the git-daemon-run provides the git-daemon command.
  • Maelvon
    Finally, found my way with :

  • Maelvon
    Where does come from the "git-daemon" command ? If you do not install the git-daemon-run package. I'm on Debian, and cannot figure out to make it run, instead I've create the /etc/init.d/git-daemon script.
    $ sudo -u git git-daemon --reuseaddr --verbose --base-path=/home/git/repositories/ --detach
    sudo: git-daemon: command not found
  • David
    nice guide thanks
  • Theome

    I've tried your instructions but I cannot get the git repositoty working. I get an "unable to chdir or not a git archive" error

    When I look the logs, I see that git-daemon is unable to allocate the listen port. Do you have any idea why this happens?
blog comments powered by Disqus