Raspberry Pi as Private Git Server

Raspberry Pi Logo

I recently set up my Raspberry Pi as a private Git server for all my Git repositories. It took a little time to get everything working correctly, but it is up and running.

Here is what you need to do in order to setup your own private Git server for all your repositories.

Install Git

First you have to get Git on your pi. sudo apt-get install wget git-core

This will install the Git server and the necessary client software.

Install SSH

If you don’t already have SSH installed, do so with the following command:

sudo apt-get install ssh

You can then start it with this command:

sudo /etc/init.d/ssh start

You now have SSH running, but once you reboot your Pi, you will have to run the command above again. You can get around this by running this command once:

sudo update-rc.d ssh defaults

After rebooting your Pi, SSH should now startup automatically. To see if you can connect to your Pi via SSH, go on your Windows machine and try connecting using Putty (Mac and Linux users can use their built int terminals to use SSH).

Putty

Enter the IP adress of your Pi in the Host Name textbox. To find your IP address run this command:

/sbin/ifconfig

Look for inet addr: and your IP is right after that.

Change Hostname

This step isn’t necessary, but I do highly recommend it if you are using, or ever plan on using, multiple Raspberry Pi devices.

sudo leafpad /etc/hostname

Type in the hostname your would like use, and then save the file. My hostname is gitpi.

Next, type the following:

sudo leafpad /etc/hosts

Replace any instance of raspberrypi with the new host name you entered in the previous step. After this, reboot your Raspberry Pi.

Add a “Git” User and Group

Next you will want to create a git user and a new user group. Note that /home/git is the directory I am using for this example. If you want it somewhere else, replace the /home/git part in the command below.

adduser --system --shell /bin/bash --gecos 'git version control by pi' --group --home /home/git git

After you have created the user, change the password.

passwd git

Your git user now has a new password. Try switching users, and you should see the terminal user and host as [email protected]. You can change users by doing this:

su git

Add an Empty Git Repository

We will now create an empty Git Repository. First change directories to where you will store your git repos.

cd /home/git

Make a directory for your repo, move into it, and initialize and empty repository. Please note that I am current the user git while doing this. This user has control over the /home/git directory.

mkdir test.git
cd test.git
git --bare init

Push Code to your Pi

Finally, we will push code onto our device. First, change directories to where you have previously initialized a git repo (or initialize a new one).

Add a new remote: (*no braces around your IP address)

git remote add pi git@[your IP]:/home/git/test.git

Now all you have to do is add your code, commit, and push.

git add .
git commit -am "Initial"
git push pi master

If you get a message about the “authenticity of host …” just type “yes” and continue. Hopefully, if everything worked out for you, you have successfully setup your own personal Git Repo on your Raspberry Pi.

If you want to test it out, try cloning your repo onto your Windows machine. First change directories to where you would like to clone it (an empty directory), then in the Command Prompt (or git bash), run this:

git clone git@[your IP]:/home/git/test.git

If you have any questions, leave them in the comments below. Good luck!