DEV Community

Chris McCormick
Chris McCormick

Posted on

Git hacks: self-host a minimal Git repo over SSH

Did you know it's super easy to self-host a git repo over SSH? If you're not looking for a full web based git interface (like GitHub, Gitea etc.) you can use the steps below to set up a simple repo over SSH. All you need is SSH access to a server that you own. You can set up as many self-hosted repos as you like. It can even be as simple as a raspberry Pi on your local network.

There are two simple parts to this. First you set up the remote server one time only. Next you add the remote server as a remote for your local repo. After that you can just git push as normal.

Remote setup

To get started SSH into the box where you want the repo to be hosted:

ssh user@example.com
Enter fullscreen mode Exit fullscreen mode

Next you're going to create a folder for the repo, cd into it, and create a bare git repo:

mkdir my_repo_name.git
cd my_repo_name.git
git init --bare
Enter fullscreen mode Exit fullscreen mode

If you want to also serve a read-only copy of this repo over HTTP, then make sure the folder is in a public web root, and run this:

git update-server-info
Enter fullscreen mode Exit fullscreen mode

Local setup

Now you can add this git remote to your existing git repo just like normal:

git remote add origin user@example.com:my_repo_name.git
git push -u origin master
Enter fullscreen mode Exit fullscreen mode

There you go, you now have a self-hosted remote you can push to over SSH.

If you want this to be a backup repo only you can add it with a name other than origin like this:

git remote add backup user@example.com:my_repo_name.git
git push backup master
Enter fullscreen mode Exit fullscreen mode

After this any time you want to push to backup instead of origin you just do: git push backup.

Enjoy your self-hosted private git repo!

P.S. If you're looking for a hosted private git repository with a web interface check out hostedgitea.com.

Top comments (0)