Jump directly to main content

Git server setup



Self-hosted version control is great way to not be dependant of a third party to keep your git server up, or your code secure.

Install Git

Many Unix operating systems have git installed by default, but if not it's a simple command.

sudo apt install git

Create a git user

This user will be used to push/pull all your git repos

sudo useradd -m git -d /srv/git -s /bin/bash

Change /srv/git to the location you wish to hold your repositories.

Create a repo

With the user, and directory created next you'll need to create a repo.

This step will need to be repeated for each new repo you create.

git init --bare repo.git

Enable SSH

Next you'll need to set up SSH for the git user.

Install and Enable SSH (If not already done)

sudo apt install ssh && sudo systemctl enable ssh --now

Create ssh key for git user

ssh-keygen -t ed25519

Use the git server

With all the setup out the way, the git server is now usable as a remote for any of your git repos.

So on another PC, you can use git as normal. For example.

Add remote to existing repo

git remote add origin git@<your-server>:/<repo.git>

Clone the repo

git clone git@<your-server>/:<repo.git>

Different SSH port

If you are using a different ssh port for your server hosting git, you'll need to add the following after the colon (:) to the two previous commands

<port>

Giving you something like this

git clone git@<your-server>:<port>/<repo.git>

Completed

From here you can use git as you would via any other provider, but with the knowledge that your remote is yours.