Hosting your own Git repositories

As you may have already heard, GitHub just got less independent at Microsoft after CEO resignation. What does this mean? Well, I guess nobody knows for certain but it probably will mean that GitHub would degrade as a service. As a business, profit and growth are the main focus of Microsoft, so everything else becomes secondary including developer satisfaction.

I will most likely start to use Codeberg as an alternative at some point and will check out other platforms like Gitea and sourcehut. While I was looking for alternatives, I wondered if there was a way to simply host Git repositories. It turns out that you can use git init --bare to create a bare repository that you can push to! If you have SSH set up, then you can dedicate some computer to hosting your repos! I'll demonstrate this approach for hosting your own Git repositories using my Raspberry Pi 5 as the host; authentication has already been set up using a SSH key.

First, SSH into the computer that will host the repos (i.e., Raspberry Pi 5) and create the bare repository.

mkdir -p ~/git/test.git && cd $_
git init --bare

That's it for the host! Now on the (other) computer where you want to push your code, create an entry for your host in ~/.ssh/config if you haven't already. Change the arguments below accordingly; the IP can be a domain name if you have the lookup set up.

Host mygit
    HostName IP
    User dave
    Port 1234

For this demo, we'll create a new repository, add the remote (to the host), and push! Just make sure that the remote path matches your hostname in ~/.ssh/config and the location on the host, i.e., the directory where git init --bare was run on the Raspberry Pi 5.

mkdir test && cd $_
git init
echo Hello > README.md
git add .
git commit -m 'First commit'
git remote add origin mygit:/home/dave/git/test.git
git push -u origin main

And that's it!

Now on yet another computer (assuming you have the same ~/.ssh/config setting), you can clone the repo from the host and work on it as you normally would!

git clone origin mygit:/home/dave/git/test.git
cat README.md
Hello

If you pushed changes on another computer and didn't get a chance to pull before editing, i.e., creating a merge conflict, they are handled as per usual since we are using Git after all.

CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.

This seems like the easiest way to host Git repositories and just relies on having SSH and Git!




Creative Commons License
This work is licensed under a Creative Commons
Attribution 4.0 International License
.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.