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.
client $ git clone user@server:~/repos/project.git
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.