I’ve got for several years a virtual server to put my own projects under version control. I started with CVS, then migrated to SVN and now I’m start thinking to migrate all the old projects to git. This because I like git very much and I use it personally for several years now.
The first question but was: “Not invented here”-syndrom? Why not using github or codeplex or any other public platform which offers git support. Well, some of my own little projects are just not worth it to publish or some of them are not meant to be published. So still existing the question why not buy a private account at github or somewhere else. At this point I was much more interested to see how hard it is to build your own little github environment. So, the requirements are:
- remote repository for git vcs
- simple user management
- website to browse the repositories
Fortunately the documentation for git is currently very good, specially this site helps a lot.
Setup a new repository on the server
First you need of course git installed on your server. The second important thing is to add a new user “git”. You can add the user by command line or through your preferred tool, like webmin. The user git is just a service account and it should not be allowed to log in with this account. So therefore just change the shell to “/usr/bin/git-shell”.
To push some changes to my git-server, I need to initialize the repository on the server. It is a convention that the directory name of a bare-repository has the suffix “.git”. So the command looks like this:
git init --bare Project.git
After that I updated the rights for the created directory:
chown -R git.users Project.git
I decided to use the simplest way to communicate with the remote repositories which is by ssh. To add a new user, you must have his public key (here a short documentation how to generate a new public key). When you received the public key of a new user, it is easy to add it to the allowed users:
cat publickey_id_rsa.pub >> home/git/.ssh/autorized_keys
Browse the git repositories
The next step was to fulfil the website requirement. I chose gitweb, which is just good enough to start and it fulfils my current needs. On my linux (debian) machine, it was an easy task:
apt-get install gitweb
Also the configuration of gitweb was really easy. First I defined a root directory, where I plan to store all my git repositories. This directory I had to specify in the configuration file /etc/gitweb.conf:
$projectroot = "/vcs/git";
The last step was to protect the gitweb site with basic authentication (I use an apache web server, so the following lines are in the configuration file of the gitweb site):
AuthType Basic AuthName "gitweb" AuthUserFile /www/gitweb.example.com/auth.db require valid-user
Finally I added the allowed users in the auth.db file.
Using the remote repository
The easiest case is to clone the remote repository:
git clone firstname.lastname@example.org:/vcs/git/Project.git Project
Another way is to add the remote repository to an existing git repository:
git remote add origin email@example.com:/vcs/git/Project.git
To get the latest changes from the remote repository you’ll use the command
git pull origin master
To save the local changes in the remote repository you’ll use the command
git push origin master