Self-Hosting Git Repositories pt. 2
Using a Virtual Private Server
This is a straightforward approach to taking firmer ownership over one's git repositories. Have a look back if you'd like to read why self-host git.
Setting up a git repository on a VPS
We'll set the stage with the following: SSH has been enabled and set up for a user, alexa
on a remote host at address remote.host
. The server also has git
, and generically understand that I can run the following commands without error.
The general idea is to initialize a remote git repository on the remote server and locally designate the created file as a git remote. It will work a lot like GitHubLab from there, minus the user interface. The only leap of intuition here is that you/I could create the host machine rather than rely on GitHubLab's server resources. Great, here are the steps for Ubuntu 22.04.
$ ssh alexa@remote.host
$ sudo groupadd gitgroup
$ sudo usermod --append --groups gitgroup alexa
$ cd /opt
$ sudo mkdir -m 0770 git
$ sudo chgrp gitgroup git
$ sudo chmod g+s git
$ cd git
$ mkdir project.git
$ cd project.git
$ git init --bare --shared
$ exit
# now on localhost
$ cd /path/to/project
$ git remote add remotename alexa@remote.host:/opt/git/project.git
$ git push remotename HEAD
All goes well, you now have a self-hosted remote git repository. You'll now be able to use git command, such as git clone alexa@remote.host:/opt/git/project.git
, to interact with the remote repository much like one of the enterprise solutions.
Given this exact setup, make note of a couple of requirements. First, any user must be able to authenticate with the host via SSH, and further if multiple users are accessing this git repository, the value of the remote URL will be different. Specifically, the value before the @
will be a username used to SSH onto the server normally. Next, a user must be in the gitgroup
group to be able to clone or push changes.
—
If you're anything like me, you like to know what any command you are running actually does. For details on what each of these steps is doing, see the follow-up article, In-Depth Commands.
Written: April 1, 2024
Last updated: January 12, 2025