Hosting Git repositories is easy and if you have SSH access to a server you can be up-and-running in under 10 minutes.
A note on git
This tutorial is intended for people with a good working knowledge of git. If that doesn’t sound like you, no problem, git is easy!
Atlassian have a good series of git tutorials for getting up-to-speed and if you dedicate an afternoon to it you’ll probably learn everything you need to know for now.
- SSH access to a server
- Git installed on your computer and server
git --versionwork in terminal, you’re golden
- Public key is appended to
~/.ssh/authorized_keyson the server
Create the remote repository
- SSH to the server
- Create a directory for your repos, e.g.
cdinto this directory
- Initiate a bare repository with
git init --bare REPO_NAME.git
bare flag means the repository won’t have a working tree, which is appropriate for a server on which you won’t be editing code.
It’s also good practice to add the
.git extension to bare repos.
Cloning your repository
If all is well thus far, you can clone the repository using ssh protocol.
Assuming your username is “rich”, the server is accessible via ssh at 192.168.1.42 and your repository is located at
~/git/repo, you can clone it using the following command:
git clone ssh://email@example.com:~git/repo.git
You may get a warning about cloning an empty repository — you can ignore it. It’s empty because you’ve only just created it!
You should now be able to make changes,
push at will.
Configuring write permissions
Some source code hosts give you some control over who can do what within your repository. A common requirement is to limit who can push to the
master branch but unfortunately even this simple requirement can’t be met with the git command line alone.
Like anything else in linux, the ability to pull and push are based on the read and write file permissions of the repository on the server, so you can create workarounds that take advantage of this.
For example, if you wanted to allow only a handful of people to be able to push to master, create two remote repositories, one for “developers” and one for “admins”. The
master branch on the developer repository is just integration, whereas the same branch on the admin repo is the actual master code.
Thus, you can add “admins” to the group that owns the repo and set the permissions to 664 (e.g.
chmod -R 644 ~/git/repo.git) so only certain people can push to it by setting the remote of the developer repo to the admin repo and pulling.
A bit convoluted, but it’s a cheap workaround that’s surprisingly-easy to manage and costs you nothing.
A simpler way?
If you know a simpler way to host your own git repos, let me know in the comments below!