Host your own private git repository via SSH

Summary

For those situations where you just need to push your code somewhere.

If you are like me you have tons of projects you would like to keep private but track with git, but do not want to pay a git host for a private plan. One of the problems is that most hosts scale their plans by project instead of users. Luckily, it is easy to host your own git repositories on any el cheapo host that provides ssh access.

In the interest of full disclosure, I learned this trick from this blog post. I decided to recreate it in case the source material vanishes for some reason.

To setup your host, login via ssh and run the following commands:

mkdir ~/git/yourprojectname.git
cd ~/git/yourprojectname.git
git --bare init

Then in your project directory (on your local machine):

# setup your user info
git config --global user.name "Firstname Lastname"
git config --global user.email "your_email@youremail.com"
 
# initialize the workspace
git init
git add .
git commit -m "initial commit"
git remote add origin ssh://youruser@yourhost.com/~/git/yourprojectname.git
git push origin master

It’s that easy!

To keep from entering your password every time add your public key to the server:

Generate your key with ssh-keygen -t rsa on your local machine. Then add the contents of the generated file to ~/.ssh/authorized_keys on your server.

Avatar
Kerry Wilson
AWS Certified IQ Expert | Cloud Architect

Coming from a development background, Kerry’s focus is on application development, infrastructure and security automation, and applying agile software development practices to IT operations in the cloud.