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 (1)

Collapse
 
headhunter45 profile image
headhunter45

Let's say I've created a repo locally and I want to push it to a new repo on some file server that I don't have proper shell access to, but I can scp to and from, and I can git clone via ssh and push changes. Maybe I have my shell set to /bin/false or something like that. Is there a better method to create a remote repo than creating a bare repo locally and using scp to copy it to the file server? I tried setting the remote url to a path that didn't exist yet, but that just fails to push because the path doesn't exist yet.

It feels like there should be something better than this.

git init --bare empty.git
scp empty.git user@server:/git/new_project.git
cd new_project
git remote add origin user@server:/git/new_project.git
git push -u origin main
Enter fullscreen mode Exit fullscreen mode