Hosting git repositories on Dreamhost
Written by Arvinder Singh / August 26, 2010 / 4 mins read / Filed under Ruby, / Rvm
A friend of mine, who like me, has a Dreamhost account, was having issues hosting git repositories over SSH on DH.
Although I like github, it might be a good idea to setup a private git repository. Following pointers from a two year old post [here](http://craigjolicoeur.com/blog/hosting-git-repositories-on-dreamhost “Hosting Git Repositories on Dreamhost | tail -f development.log”), I was successfully able to setup a personal web hosted git repository. |
Step1. Make sure your host has installed git. A way to check is to ssh into your host and pass this command:
git --version # My ISP shows git version 1.7.*
Step 2. Create a new bare Git repository
I created a seperate subdomain for git git.example.com.
Find absolute path to this directory root using pwd -L
command. It is referred throughout this tutorial as $PATH. Also $USER is your username.
Automate building repositories adding this to your ~/.bashrc
file.
newgit()
{
if [ -z $1 ]; then
echo "usage: $FUNCNAME project-name.git"
else
gitdir="/home/$USER/$PATH/$1"
mkdir $gitdir
pushd $gitdir
git --bare init
git --bare update-server-info
cp hooks/post-update.sample hooks/post-update
chmod a+x hooks/post-update
touch git-daemon-export-ok
popd
fi
}
This way you can simply login to your DH account and type:
$ newgit my-new-repos.git
and a new bare repository will be created for you in your $PATH. Or if you prefer to create a new repository from your local box you could even do it over SSH:
$ ssh $USER@$MACHINE.dreamhost.com newgit my-new-repos.git
Step 3. Pull and push to your new repository
There are issues pulling and pushing empty repositories. I also came across this excellent article pointed from the given link, which helped understand the following process.
Checkin your code into a local git repository:
$ cd /my/code/path
$ git init
$ git add .
$ git commit -m "initial import"
That will create a local git repository in your working directory and commit the initial files. Next follow the next set of commands to add your DH repos as a remote and push your initial codebase up to it.
### assuming ssh://server/remote.git resolves to an empty, bare git repo
### and that we are chdir()'d to the local repository:
$ git push --all ssh://$USER@$MACHINE.dreamhost.com/home/$USER/$PATH/my-new-repos.git
$ git remote add origin ssh://$USER@$MACHINE.dreamhost.com/home/$USER/$PATH/my-new-repos.git
$ git config branch.master.remote origin
$ git config branch.master.merge refs/heads/master
$ git fetch
$ git merge master
$ git branch -a
* master
origin/master
Now you can simply work locally and do a
$ git push
to push to your DH remote. And if you ever need to you can simply execute a
$ git pull
to pull down and merge remote changes.
Step 4. Setup GitWeb (optional)
If you want to be able to access your git repositories via a web browser, you need to setup GitWeb. GitWeb ships with the git source code so you already have it available.
Download the corresponding version of git source code and untar it in a temporary directory ~/src. Now find location of git on your server by running this command
$ which git
git is /usr/bin/git
Build gitweb.cgi file using
$ cd ~/src/git-1.7.1.1/
$ make prefix=/usr/bin gitweb/gitweb.cgi
Now you just need to copy the GitWeb files into your git repository directory and make them executable.
cd <PATH>;
cp ~/src/git-1.7.1.1/gitweb/git* .
rm gitweb.perl
chmod 755 gitweb.cgi
Its a good idea to create a configuration file as well:
touch gitweb_config.perl
vim gitweb_config.perl
and put this as the gitweb_config.perl
content:
# where is the git binary?
$GIT = "/usr/bin/git";
# where are our git project repositories?
$projectroot = "/home/$USER/git.example.com";
# what do we call our projects in the gitweb UI?
$home_link_str = "My Git Projects";
# where are the files we need for gitweb to display?
@stylesheets = ("gitweb.css");
$logo = "git-logo.png";
$favicon = "git-favicon.png";
# what do we call this site?
$site_name = "My Personal Git Repositories";
Then to get it to actually run in the browser you need to tell apache to serve the files up via CGI. Edit your .htaccess
file with the following info:
Options +ExecCGI
RewriteEngine On
RewriteRule ^$ gitweb.cgi
RewriteRule ^([?].*)$ gitweb.cgi$1
Now you should be able to access your git repositories from you web browser at http://git.example.com.
I personally went one step further and password protected my repositories. If you don’t want your git repositories exposed to the whole world, you might want to do the same thing. I used simply HTTP authentication by adding the following to the bottom of my .htaccess
file:
AuthType Basic
AuthName "Private Git Repository Access"
AuthUserFile /home/$USER/.htpasswd
Require valid-user
This, of course, requires that you have proper .htpasswd
file setup which you can also enable using web interface on most ISP’s.