Menu Close

How to Self-Host Git Server on RaspberryPi

Self-Hosting a git server on a raspberryPi

In this tutorial, we are going to cover how to self-host a git server on a RaspberryPi. Git is an awesome tool for version controlling code that you write. While you can use git locally, you will want to use a git server to get the full benefits. This can even be used to allow your friends to work remotely with you on the codebase by setting up a VPN server on the same RaspberryPi hosting the git server.

Table of Contents

Overview

Self-Hosting a git server is incredibly easy on a RaspberryPi, and it will unlock the ability to use git to its full capabilities. I personally host my own git server and use it for all of my code development. This not only allows me to keep all of my code updates consistent across all of my devices, but it also allows me to make updates on my computer and then easily push them to my RaspberryPi. This process allows me to bypass writing code updates via nano over SSH, and allows me to use atom, my favorite text editor, to make the updates.

For more serious code projects, I would highly recommend storing the git repository on a USB flash drive, rather than on the RBP’s SD card itself. SD cards are notoriously susceptible to card failure, and nothing is worse than loosing your code history! Luckily, due to the way that git works, if you did wipe out your git server, you would still have all of your code as it is stored locally on each computer using the code base.

In order to allow remote users access to this git server, you can also setup a VPN server on the same RaspberryPi. You would then give each user both a VPN profile and a user account on the RaspberryPi. By changing the folder permission of the git repository, you can change who has access to each specific project.

How To

Installing git

To install git on the RaspberryPi, first SSH into the pi and type:

sudo apt update
sudo apt install git

This will update your package links and then install git using apt. You may be given a confirmation. If so type y and return.

Creating a repository

Initialized Git Repository

I would recommend creating your repository on a USB drive to lower the chance of drive failure. If you do this, set its mount point to the /repo we are creating.

We are going to create a directory at the root level in which to put all of our repositories. To do this type:

sudo mkdir /repo

General users will not have access to this repository. If you are planning on selectively giving users permission to each repository, skip the next step. Instead either create applicable groups or set individual permissions for each repository. Note: you still can add your own private repository later on by changing the permissions to only allowing your user access.

To give all users permissions to the projects within this directory, type:

sudo chmod -R 777 /repo

Now all users will have permissions to this directory.

For each repository you create, you are going to want to create a new directory in /repo by typing:

mkdir /repo/cats

Now to initialize this directory as a bare git repository, type:

git init --bare /repo/cats

You now have an empty repository to clone from!

Utilizing Git

This section will only cover the basics of using Git. There are tons of other tutorials out there that give full instructions.

The first step in using git is to clone this repository on whatever device you are going to be using. To do this on Mac / Linux, open terminal and cd into the directory where you would like to save your code projects. On Windows I would recommend using TortoiseGit. The following instructions are for Mac / Linux:

To clone the directory type:

git clone {user}@{IP}:/repo/cats

replacing user and IP with your pi user name and Pi’s IP address respectively.

This should now ask you for the password to the RaspberryPi. Enter it and you should get an empty repository!

This is now live and ready to go as a git repository. Use your favorite GUI to commit and push to the repository! If you are not sure where to start with git I would recommend using Atom as it is incredibly easy to get started.

To avoid having to type your password every time you push or pull from the server, you can instead use SSH keys.

Conclusion

Overall, git is an awesome way to version control your code, and the use cases are really endless. By self-hosting your own git server on a RaspberryPi, you have the ability to utilize git to its fullest potential.

Git can be slightly overwhelming at first, which is why I would highly recommend starting by using a GUI. There are hundreds available that all have their own use cases. If you are writing code, I would look at starting with Atom because it is a really easy to use text editor!

Thanks for reading. Let me know if you have any questions or ideas for future tutorials in the comments below!

Posted in RaspberryPi

Related Posts

%d bloggers like this: