DEV Community

CecΓ­lia Coelho
CecΓ­lia Coelho

Posted on

How to host a worldwide private Git server 🌍 on a Raspberry Pi - clearly explained (beginner's friendly)

In this beginner's friendly tutorial we will be setting up a Raspberry Pi (aka Pi) to host your very own private Git server that will be staying in the comfort of your home 🏑. This way you can have your (very important highly secret 😈) projects backed-up and accessible without relying on external agents (like GitHub, GitLab, etc) in 5 simple steps:

  • 1-Git installation
  • 2-Mounting a USB drive
  • 3-Initializing a Git repository
  • 4-Add/Commit/Push into your Raspberry Pi
  • 5-Using your Git server anywhere

Before we start, I'm assuming you already have setup your Pi and have SSH enabled. If you don't follow this guide: https://dev.to/ceciliacoelho/how-to-setup-a-headless-raspberry-pi-clearly-explained-beginner-s-friendly-4aph

πŸ“ What we will be using:

  • Laptop
  • Raspberry Pi 4
  • USB (any size you need)
  • Internet Router

Step 1: Git installation

SSH into your Raspberry Pi using your laptop (or use VNC Viewer and open the terminal in the desktop). First we are going to make sure everything is up-to-date,

sudo apt-get update
sudo apt-get upgrade
and reboot sudo reboot.

Again, access your Pi to install Git,
sudo apt-get install wget git
When it's finished turn off the Pi.

Step 2: Mounting a USB drive

Plug your USB drive into the Raspberry Pi (make sure it's formatted as FAT) and turn the Pi back on and SSH into it.
Now we are going to create and assign a directory to this USB so every time we want to access our projects we know exactly where they are πŸ“Œ. To create the directory do
mkdir usbdrv
and now we need to map the USB to it. To do that first let's get the USB info, do
sudo blkid
you should get something like this:

/dev/mmcblk0p1: LABEL_FATBOOT="boot" LABEL="boot" UUID="5DE4-665C" TYPE="vfat" PARTUUID="225e7479-01"
/dev/mmcblk0p2: LABEL="rootfs" UUID="7295bbc3-bbc2-4267-9fa0-099e10ef5bf0" TYPE="ext4" PARTUUID="225e7479-02"
/dev/mmcblk0: PTUUID="225e7479" PTTYPE="dos"
/dev/sda1: LABEL="GITPI" UUID="F234-F2DF" TYPE="vfat" PARTUUID="0f6f2c0f-01"
Enter fullscreen mode Exit fullscreen mode

When I formatted my USB I gave it the label "GITPI" so the last line of the above block corresponds to the information I'm looking for, you should have something similar for yours.
Let's tell the Pi that our USB contents should go to the created "usbdrv" directory by editing the "systems table" file, do
sudo nano /etc/fstab
A file should have popped up in a text editor, in the end of the file add this line (my USB was at /dev/sda1 as you can see in the output of sudo blkid, make sure to replace by whatever you got):
/dev/sda1 /home/pi/usbdrv vfat uid=pi,gid=pi,umask=0022,sync,auto,nosuid,rw,nouser 0 0
to save and close the file Ctrl-x -> Y -> return.
Once more, it's reboot time! πŸ˜…

Step 3: Initializing a Git repository

Now go to your laptop where you "work" **cough cough** πŸ™„, you can create a blank folder to start a new project or you already have one full of juice πŸ˜‰, the step is the same. Initialize a Git repository in the folder using git init.
Back to the Pi πŸ₯§, create a folder with the .git extension to hold the repository, do (replace projectname by the name you want to give your repository)
mkdir usbdrv/projectname.git
Go into the directory you just created
cd usbdrv/projectname.git
and create an empty repository,
git init --bare
you should get this output:

Initialized empty Git repository in /home/pi/usbdrv/projectname.git/
Enter fullscreen mode Exit fullscreen mode

❗❗ If you get permission denied while executing these commands try and add sudo to the beginning of each.

Step 4: Add/Commit/Push into your Raspberry Pi

Finally the magic is going to happen!!!!! πŸ§™β€β™‚οΈ

Go to your laptop and create a file if the folder is empty, otherwise we already have stuff to push into our private Git, how exciting! 😁
First we need to add the remote, do
$ git remote add pi pi@192.168.50.166:/home/pi/usbdrv/projectname.git
where the first "pi" will be the name we'll be using when pushing. Don't forget to plug in your Raspberry Pi address (the same you use to ssh) and the name of your git folder (created in step 3).

Now, for the main event, the Add/Commit/Push triathlon! πŸŠβ€πŸš΄β€β™‚οΈπŸƒβ€β™‚οΈπŸ’¨

  • git add .
  • git commit -m "first commit: is this really working?"
  • git push pi master It is going to ask for your Raspberry Pi password for the user you are currently using, I'm using pi. If everything went well you should get this output:
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 222 bytes | 222.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To 192.168.50.166:/home/pi/usbdrv/projectname.git
 * [new branch]      master -> master

Enter fullscreen mode Exit fullscreen mode

Looks like a success right? πŸ€”
If you are like me and you want to be sure your stuff is really on that USB and make sure if something happens with your Pi you can just plug the USB on any laptop and you'll have access to everything then stay with me!🧐

  • Turn off the Raspberry Pi and unplug the USB;
  • Plug the USB into your laptop;
  • Go to the USB and open everything your eyes see; 😦
  • Fail to find anything that looks like what you just push; 😨
  • Panic! 😱😱😱😱😱

Wait a second!

The USB has a Git repository so you won't be able to see your files like that 😏.
Use the terminal to navigate to somewhere you want to put your stuff in and do,
git clone pathToUSB/projectname.git
This command will clone your repository and when it's over you should see what you were expecting, your files! πŸ€—

Step 5: Using your Git server anywhere 🌍

The steps explained above will only work in case the laptop you are using to access the Git server is on the same network of the Raspberry Pi hosting it, meaning both have to be connected to the same router (via Wi-Fi or LAN).

HOW LAME! πŸ˜’

What we want is a Git server that we can use anywhere, outside and inside the Raspberry's network. To do this we need what is called port forwarding and the steps to setting it up will depend on the router you have. Note that port forwarding is considered dangerous since it gives access to your local network from the outside, make sure you use strong passwords at least.
The steps to perform the above instructions on your router can be found in the user manual or online:

  • Assign a static IP address for the Raspberry Pi: this will prevent us from losing the address of the Pi due to router reassignment. To do this you need to access your router's through a web browser by using its IP address and navigate to the page where you can manually assign IP, Figure 1.
Figure
Figure 1. Page to assign a static IP address.

After adding the Raspberry Pi, reboot.

  • Port forwarding: launch the router's configuration web page again and look for the port forwarding page, Figure 2.
Figure
Figure 2. Port forwarding page.

Fill in the required information and make sure the Port is 22, this is the SSH port used by the Raspberry Pi, and use Protocol TCP.

Now we will setup the remote of the git repository to be the universal address of the Pi, that you will be able to access from anywhere! Go back to the project you setup in step 4 and do (where "222" is the value you chose for Port Range):

$ git remote add remotepi pi@192.168.40.16:222/home/pi/usbdrv/projectname.git

Everything is set now, get on the next plane to wherever and let's try! πŸ›«πŸ˜Ž

  • git add .
  • git commit -m "commit from the North pole!" β›„
  • git push remotepi master

That's it, first push outside the Raspberry Pi's network!

Success! You are now ready to build some awesome projects!!! 🀩πŸ’ͺπŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰

If you run into any problems just leave a comment. πŸ˜‰

Discussion (0)