With the enshittification of Github and potentially, Bitbucket with AI tools and changes in terms of services where the owners of those platforms could decide to start training their AI models on proprietary code, I decided that it was time to host my own git server at home.
This post is to set it up on a Debian system. This is not using any of the web-UI front-ends like gitea or gogs or gerrit. This post describes setting up the bare-bones git server and migrating the old repositories to this git server.
A web-UI like those mentioned above are not necessary to use git. They can be useful for code-review if your team is larger, but for one-person teams like mine, I do not need a web UI unnecessarily running.
Setting it up on a Debian or Ubuntu system is very easy and this post will show you how.
This post will also show how to use ssh based authentication and is not for http based authentication. ssh based authentication is more secure.
PRINCIPLE
A git repository is just a set of files placed in a directory that track changes in source code.
When you run a git clone git@IP_ADDRESS:myrepo.git all it is doing is making an ssh connection to the server whose IP address is IP_ADDRESS and looking for a git repository named myrepo, and then making a copy of it locally by downloading the appropriate files over ssh.
It’s quite simple actually. If your server already has ssh availability, you do not need anything more to create a git service.
SETUP SSH
Let’s assume you have a Debian or Ubuntu server running, and have the latest git, openssh-client and openssh-server packages installed. I tested this with Debian 11, but any version should work.
This will be the server that hosts the git repository for all users.
We first create a new user called git on this host server. Let’s assume the IP address of this host server is IP_ADDRESS. You can also use a hostname instead.
### on the git server
$ sudo adduser git
$ id git
uid=1001(git) gid=1002(git) groups=1002(git)
$ sudo su - git
$ cd /home/git/
$ mkdir .ssh && chmod 700 .ssh
$ touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys
The uid and gid maybe different for your host server.
This should create the git user which will be used to ssh into the server.
If you already have a Bitbucket or Github account, you can re-use your ssh key for this step, or generate a new one as below.
Here we are choosing the key file name as local_git.
## on the work system
$ ssh-keygen -t ed25519 ~/.ssh/local_git
$ ssh-copy-id -i ~/.ssh/local_git.pub git@IP_ADDRESS
Now that you have added the ssh keys to the host server under the git user, for convenience you should update the .ssh/config file for easy access to the host server.
Open the file ~/.ssh/config and enter the following contents, editing it apppropriately for your use case.
### on the work system
Host localgit
HostName IP_ADDRESS
User git
Port 22
IdentityFile ~/.ssh/local_git
You should now be able to perform an ssh localgit and get access immediately to the host server without any passwords.
Now let’s setup the git repositories.
NEW REPOSITORIES
Setting up new repositories is really easy.
Let’s say you want to setup the repositories on the host server in the home folder like /home/git/.
Let’s say your project name is myproject. You can do the following on the host server:
### on the git server
$ cd /home/git/
$ mkdir myproject.git
$ cd myproject.git
$ git init --bare
This will initialize an empty repository for your project. Now on the work computer, where you want to clone this repository you would do the following:
### on the work system
$ cd myproject
$ git init
$ git add .
$ git commit -m "First commit"
$ git remote add origin localgit:myproject.git
$ git push origin master
Now you have setup your first repository and pushed a commit to it.
MIRROR EXISTING REPOSITORIES
Since this post is about migrating the Bitbucket or Github repositories to your personal server, we explain that now.
Let’s say your Github repository is at https://github.com/myuser/myrepo.git, where your Github user is called myuser and your repository is called myrepo.
To mimic the same structure, on the git host server you will run the following:
## on the git server
$ cd /home/git/
$ mkdir -p myuser
$ cd myuser
## For Github repositories
$ git clone --no-remote-submodules --mirror git@github.com:myuser/myrepo.git myrepo.git
## For Bitbucket repositories
$ git clone --no-remote-submodules --mirror git@bitbucket.org:myuser/myrepo.git myrepo.git
The last step is the most important above. It is making a full mirror of the git repository of the myrepo from Github. The steps are the same for Bitbucket.
You will need to setup access to Github or Bitbucket obviously to be able to mirror this repository locally.
This mirror is now ready for cloning from your work computer.
## on the work computer
$ git clone localgit:myuser/myrepo.git
That’s if you want to do a fresh clone of the repository.
However, if you want to edit the existing URL of a working copy of the repository, you can run the following command:
## on the work computer in an existing repo
$ cd myrepo
$ git remote set-url origin localgit:myuser/myrepo.git
Now it is done. You can do a git pull to verify all this works.
That is it. This is the setup. You can migrate every repo from Github or Bitbucket into your private ssh server, either hosted at home or on a cheap $5 VM on the cloud by yourself.
NOTE: Remember to disable password authentication for ssh once your keys have been setup.
ADDING MORE USERS
To add more users who can commit code to the git repositories, you can either add additional ssh keys to the git user on the host server as shown earlier in this post, or use a web-UI for managing the same process.
$ ssh-copy-id -i ~/.ssh/new_user_key.pub git@IP_ADDRESS
If you prefer for each user to have their own account on the host server, that is possible as well.
- You would add individual users to the host server with their own keys.
- They would use their own
usernameinstead of the usergitto login to the server. So their connection string will look likemyuser@localgit. - The repositories must be at an accessible path like
/opt/gitor/srv/gitor similar that is accessible by all users. - The directories should be writable by all permitted users. You can add the permitted users to the
gitgroup by doingadduser <username> gitand then make thegitrepository folders be writable by anyone in the groupgit, by usingchmodandchownappropriately.
An example is below:
## on the git server
## make a common git repo folder for all users in the git user group
$ sudo mkdir -p /opt/git
$ sudo chown -R git:git /opt/git
$ sudo chmod 0775 /opt/git
$ cd /opt/git
## mirror repositories here
$ mkdir -p myuser
$ cd myuser
## For Github repositories
$ git clone --no-remote-submodules --mirror git@github.com:myuser/myrepo.git myrepo.git
If done as above, the work computer will do the following to update the URL:
$ git remote set-url origin myuser@localgit:/opt/git/myuser/myrepo.git
BACKUPS
Since you are hosting your own server, you will need to back it up yourself. If you are hosting on the cloud, definitely use the cheap cloud backup options available on the cloud provider.
However, if you are hosting at home on a physical server, you should definitely backup the whole /home/git folder for your repositories to an external drive or an off-site drive via a service.

Donate BITCOIN to 19hrWWw1dPvBE1wVPfCnH8LqnUwsT3NsHW.