How to use git over SSH

git is an excellent distributed version control system, originally developed by Linus Torvalds. It’s relatively simple to start using, but very powerful! Using git over ssh can also be acheived relatively easily. This is a simple guide to using git over ssh, without having to run gitosis or a git daemon etc. A nice simple solution for simple projects.

First you must choose a ‘host’ machine, this is the server that will store the main copy of your git repo. We’ll call it ‘server’. First you need to make an empty git repo. We’ll keep all our repos in the directory ~/repos/ as a normal user. (root not required).

Setup the repo on the server:

server $ mkdir ~/repos/
server $ cd ~/repos/
server $ GIT_DIR=project.git git init
server $ cd project.git
server $ git --bare update-server-info
server $ cp hooks/post-update.sample hooks/post-update

A breakdown of what just happened:
We created a new git repo, but instead of the directory being “.git” we set it to “project.git”.
Next we updated some info files. Because the repo is for a server, clients need to know where to find certain files. update-server-info takes care of this for us! Please note, this is only really required if you will be using the git repo over HTTP or HTTPS. It does no harm to add it here, but for pure SSH you can safely ignore this line.
Finally we used the sample post-update hooks file as our post-update config file.

Now you have an empty repo on your server! Now you’ll want to clone the repo on your client machine so you can start to fill it up. There are two main ways to do this, it doesn’t matter which you choose. You can either clone the repos as it is, or your can init a new repo on your local machine and set the remote origin manually. Both are the same. All commands from now on happen on the client machine ‘client’. Over ssh or phyiscally sitting at the workstation, it doesn’t matter.

By cloning:

client $ git clone user@server:~/repos/project.git

By init:

client $ mkdir project
client $ cd project
client $ git init
client $ git remote add origin user@server:~/repos/project.git/

Here “user@server:~/repos/project.git” is the address of your server plus the username and directory hosting the git repo.

Now both local repos are ready for you to add files, git add, and git commit! When you want to push to the remote repo do so as you always would! Example:

client $ touch README
client $ git add README
client $ git commit -m "Example."
client $ git push origin master

If you want to edit the repo on the server, you can just clone the repo to another directory and it will behave the same.

It’s that easy. I would recommend using keybased ssh authentication because it makes things so much easier. You can of course have as many clients as you want accessing your repo. If you’re collaborating you could add a user called git to the server and add all the developers public keys to ~/.ssh/authorized_keys2.

14 thoughts on “How to use git over SSH

  1. Troic

    I had to add this command to the client :

    git config –global remote.origin.receivepack “git receive-pack”

    to get

    git push origin master

    to work.

    Client (Centos 5.7 Final : git version 1.7.4.1)
    Server (Ubuntu 10.10 : git version 1.7.4.2)

    Thanks for the original post :)

    Reply
  2. Pingback: w3factory » Git over ssh

    1. Will Post author

      Ah, very interesting. I had not seen this before! I’ve added this information to my post above, many thanks!

      Reply
  3. Pingback: a simple git repository over ssh « YarcRamble

  4. Pingback: Installing Trac (with git) on Fedora [blogger import] | Organised Chaos

  5. pax

    sudo git push origin dev
    [sudo] password for user:
    user@192.168.0.5′s password:
    Counting objects: 9, done.
    Delta compression using up to 6 threads.
    Compressing objects: 100% (4/4), done.
    Writing objects: 100% (5/5), 629 bytes, done.
    Total 5 (delta 3), reused 0 (delta 0)
    error: insufficient permission for adding an object to repository database ./objects

    fatal: failed to write object
    error: unpack failed: unpack-objects abnormal exit
    To user@192.168.0.5:/var/www/apps
    ! [remote rejected] dev -> dev (n/a (unpacker error))
    error: failed to push some refs to ‘user@192.168.0.5:/var/www/apps’

    Why can’t I push to the remote server via ssh?

    Reply
  6. Pingback: Installing Trac (with git) in Fedora 15/16 [Part 2] | Organised Chaos

  7. Pingback: How To Instal A Git Repo & Serve Via SSH

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>