Raspberry Pi Git Server

Raspberry Pi and Raspbian

If you have not heard about Pi’s yet check it out. Raspberry site. They are single board computers running a Debian Linux distribution by the name of Raspbian.  They are pretty dang sweet. In this tutorial I’m using a Pi 3 B+. The great thing about Pi’s is that they are really inexpensive (about $35 for the board). People are doing all sorts of really cool IOT projects with them.  I’ve played around with them a little bit and have made a home entertainment player and setup some small web servers. In this tutorial though I wanted to give a rundown on how to create your own Git server. I am a fan of GitHub, but this is a way to create your own setup.

This will be a step by step to show you how to create your own private Git server hosted on your LAN. This won’t be as fancy as GitHub, but this is still going to be cool!

Update Your Pi and Enable SSH

I’ve done this a couple of ways, but I think this will be the most succinct. First, you need to boot up your Pi and open the terminal (ctrl+alt+T). Then you will run the following for your user:

pi@raspberrypi:~ $ apt-get update && apt-get upgrade

The default user is named pi.
This will update and then upgrade any packages and dependencies for your Pi.

Next run:

pi@raspberrypi:~ $ sudo raspi-config

This will bring up a new config window in your terminal. Go to Advanced Settings and look for enable SSH. This will allow you to access your pi from another linux/unix machine or windows if you want to use Putty.

Next in your terminal run:

pi@raspberrypi:~ $ sudo ifconfig

Look next to the wlan0 in your terminal output. This is your pi’s fixed ip address. Write it down to have it readily accessible.

Make sure that you have ssh server installed, I did by default but you never know. Run:

pi@raspberrypi:~ $ sudo apt-get install openssh-server

Same goes for Git. It seems like the newest release of Raspbian had both these packages installed by default.

pi@raspberrypi:~ $ sudo apt-get install git

Adding a New User for Git

I always prefer to add a new user if it is going to be performing only one task, also you don’t want to be doing everything using a root privilege user. So in your terminal run to add a user with the name git:

pi@raspberrypi:~ $ sudo adduser git

You’ll be prompted for a password for that user and then default empty on the rest of the fields for this. Next we’ll need to make this a superuser.

pi@raspberrypi:~ $ sudo usermod -aG sudo git

We now have added a user by the name of git, with superuser powers!

Try Out SSH from Another Terminal

At this point, you will need to make sure you can use ssh and actually get into your Pi from another machine on your LAN network. For this demonstration, I am running an Ubuntu 16.04 machine.  Most unix based operating systems should be similar. Windows you will need to install and use Putty to SSH or I find Git’s bash shell works well too.

Try using SSH by entering the following (Note I am now using the git user I created earlier, not pi user). This is where you will need the IP address for your pi, as well as the username that you created. I forgot to mention the IP should look similar to what’s below. This 192.168 prefix means that this is a local IP on your network (Should also be fixed, unless something on your network changes drastically).

user@machine:~$ ssh git@192.168.0.XX

If all goes well you will be told that this host’s identity can’t be established and will prompt you whether you want to continue or not. Type yes. You will then be asked for the user’s password.

If you’re refused for some reason, there are plenty of resources available, you can also leave a comment, and I may be able to help.

Mounting a USB drive

I am also mounting a USB drive that will house my Git repo’s. Doing this will allow me to still use the Pi and not worry about my Git repositories taking up all 8GB of my microSD. I went with a 16GB USB stick I had laying around. Reformat to FAT format. This will erase whatever you had on there so make sure you don’t lose anything important. Place it in one of the open USB ports on the back of your Pi. I SSH’ed in through my laptop and then ran:

git@raspberrypi:~/sudo blkid

You should see all the blocks on your machine, including the USB drive you reformatted and stuck in the back. You’ll see something similar.

/dev/mmcblk0: PTUUID="b7dcc683" PTTYPE="dos"
/dev/mmcblk0p1: LABEL="boot" UUID="0EDC-9EC5" TYPE="vfat" PARTUUID="b7dcc683-01"
/dev/mmcblk0p2: UUID="7f593562-9f68-4bb9-a7c9-2b70ad620873" TYPE="ext4" PARTUUID="b7dcc683-02"
/dev/sda: UUID="A0E4-CE98" TYPE="vfat"

The bottom block is your USB, with the TYPE=”vfat”. Now we will need to modify the fstab file to permanently mount the USB.

sudo nano /etc/fstab

Go to the bottom of the file and enter in the following.

/dev/sda /home/git/usbdrv vfat uid=git,gid=git,umask=0022,sync,auto,nosuid,rw,nouser 0 0

(Note, if your USB block looks a little different, you’ll need to modify that, could be something like /dev/sda1, or /dev/sda2, depending on which USB port you plugged your thumb drive into. Also if you’re doing this with a differently named user other than ‘git’ you’ll need to modify the uid= and gid=. This line gives that user full privileges for access to that thumb drive.

Creating A Remote Repo

If all has gone well up to this point create a another directory under usbdrv on your Pi. This will be your new repository.

git@raspberrypi:~ $ mkdir usbdrv/gittest.git

Then initialize it.

git@raspberrypi:~ $git@raspberrypi:~ $ git init --bare usbdrv/gittest.git/

You should then have a bare remote repository you can push up to.

Setting Your Remote and Pushing Initial Commit

Back on your desktop/laptop open a terminal if you haven’t one open and create a new directory.

user@machine:~$ mkdir GitTest

Then you’ll need to initialize this as a local repo.

user@machine:~$ git init /GitTest

Touch a new README.md

user@machine:~$ touch /GitTest/README.md

You should then be able to follow the rest of the example below for adding your adding and committing your file, then pushing to a new master branch.

user@machine:~$ cd GitTest
user@machine:~/GitTest$ git status
Initial commit

Untracked files:
  (use "git add ..." to include in what will be committed)

    README.md
user@machine:~/GitTest$ git add .

user@machine:~/GitTest$ git commit -m"Initial commit"

Now we will add the remote repository on our Pi to our local repo, and do our initial push.

user@machine:~/GitTest$ git remote add origin git@192.168.0.XX:/home/git/usbdrv/gittest.git
user@machine:~/GitTest$ git push origin master
git@192.168.0.XX's password: 
Counting objects: 3, done.
Writing objects: 100% (3/3), 230 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.0.200:/home/git/usbdrv/gittest.git
 * [new branch]      master -> master

This is by no means a tutorial on git, but if you have any question please leave them below.

Thanks again for reading this and I hope you have found this useful.  I always seem to learn more by doing attempting to show others how I have done things. For further information,  look into further securing your pi with these two articles covering SSH key gen and further securing your Pi.  Here is for ssh. I am actually hosting this site on DigitalOcean and here  for setting up watchdog and UFW firewall.

JavaScript Code Nesting and Lexical Environments Explained
Next On FoC

JavaScript Code Nesting and Lexical Environments Explained

JavaScript Code Nesting and Lexical Environments Explained
What is an IIFE?
Previously On FoC

What is an IIFE?

What is an IIFE?