Setting up Your Raspberry Pi as a Git Server

Share this article

In a recent article, Jérémy introduced the Raspberry Pi, a tiny computer about the size of your hand. In this article, he gives another example of the impressive uses of this amazing device.

Git to Raspberry Pi

Managing versions of a project is useful. Even if your project is private, it’s always a good thing to be able to retrieve an old version, if (for instance) the new one is broken for some reason.

To manage versions of a project, we can create a new folder for each new version but, today, we have great tools that allow us to do far better.

One of these tools is Git. Git is great for managing versions of a project, whether you’re alone working on this project or not. This tool can be used on a single computer without any problem. However, if you want to share your code between several computers, you’ll need a Git server. That’s what we’ll build in this tutorial.

First I’ll explain what we’ll build, and why we need it. Then, we’ll see concretely how to build our server with a Raspberry Pi. Finally, we’ll see how to use our server.

What Do We Want to Do?

A Git server?

Git logo As said above, you can use Git on a single computer, just to manage versions of a private project. The problems come when you want to share your code, either publicly or between two or more of your computers.

In this case, you need a place to send your modifications, so they can be retrieved on the other computers. This place is a server.

With this server, the process will be as follows:

  1. you modify your code
  2. you send your changes to the server
  3. on another computer, you or anybody else downloads the changes from the server
  4. new changes are made from this other computer
  5. these changes are sent to the server so that others can download them, and so on.

A Git server is basically a computer connected to the Internet on which we can store Git repositories. Any computer can be used for that and, here, we’ll use a Raspberry Pi, as it has the advantage of being a very low cost option, while giving good performance.

Why create our own server?

There are various services around the Web that allow us to share Git repositories—like GitHub, which is one of the most popular.

The problem is that repositories on services like these are often public. Sometimes, we don’t want to share our code publicly, but rather to share it with just a few people (or even just another computer we own).

Some services like GitHub offer private repositories — so that you can choose who sees your code — though they’re not free. That’s why creating our own server can be a good idea.

We’re not recreating GitHub here

Just to be clear, though: we’re not attempting to replicate GitHub with a simple Raspberry Pi. The server we’ll create is a very simple one, with all the features Git has, but no more. So there won’t be the facility for things like pull requests, or even a graphical interface.

Even if simple, though, this server will be far from useless. I currently have a Git server on my Raspberry Pi. I use it to share code between my desktop and my laptop. I use GitHub for some projects, but not for all of them. For some private projects, or for experimental things I don’t want to share for now, I use my little server. That way, I can work from any computer without having to publicly share my code.

What will we need?

There are several versions of the Raspberry Pi, from the very small and cheap Raspberry Pi Zero to the recent and powerful Raspberry Pi 3. Which should you choose for your Git server?

I didn’t test on a Raspberry Pi Zero, so, to be honest, I won’t be able to say if this one is sufficient (I think it is, though). However, the very first version of the Raspberry Pi is largely enough and, as the Pi 2 and 3 are available, maybe you can find the first Raspberry Pi for a small price, so it’s a good option to think about.

Anyway, the Raspberry Pi is just a motherboard. Whatever version you choose, you will need at least a case to protect it, a power source (via microUSB, like a smartphone), an SD card to store the OS (a classical SD card or a microSD following the version of the Pi you chose) and a way to connect the Pi to the Internet. Depending on the Pi you have, you can connect it through Ethernet, directly via Wi-Fi (for the Pi 3) or with a Wi-Fi key.

Now that you have everything you need for this project, let’s see how to get a Git server from this small computer!

How To Build a Git Server with a Raspberry Pi

Installing Git

The first thing our server will need is Git. You can find it in the git package, which you can install by executing the following command on the Raspberry Pi:

sudo apt-get install git

From a very basic view, that’s all we need to get a Git server. However, there are some details we can add to get a proper and more secure server.

Creating a dedicated user

It’s a good practice to create a new user for each use you make of the Raspberry Pi. This user is the official manager of its corresponding use.

Some programs will automatically adds their own users. For instance, Kodi, the media center, creates the user kodi. In the same vein, installing a LAMP solution (for getting a web server) will create the user www-data. Here we’ll create the user git. To do that, type the following command on the Raspberry Pi:

sudo adduser git

If you want to, you can change the name of the dedicated user to another one, by changing git in the above command.

You’ll be asked to enter a password for this user. Then, you’ll be able to enter some other information like the real name of the user or their phone number. As git is not a real person, you can skip all these questions without any problem.

Using SSH to access the server

Having a dedicated screen for a server is pretty much useless. Most of the time, we just want to exchange data with it. To do that we can use SSH, a secure way to communicate between two computers.

Explaining how SSH works is not the aim of this article, so I won’t describe how to use it. However, you can find excellent documentation on Raspberry Pi’s official website.

How To Use Our Server

Raspberry Pi logo

Our server is now ready to use. So it’s time to see how to use it. We’ll see here how to create a new repository, and how to update it.

Let’s show our great powers of imagination to the world by creating a test repository named Hello-World.

Creating a new repo on the Raspberry Pi

The first thing to do is initialize our new repository on the Raspberry Pi. On the server side, a repository is basically the same thing you find on your computer: a folder with a .git subfolder. So the first thing to do is to create this folder.

On the Raspberry Pi, use the git user to create it (through SSH or not, but you should always use this user to create new repositories). You can create this folder anywhere you want: on the Raspberry Pi’s SD card, for instance, or on an external HDD. In the following command, we create the folder in the home folder of git:

mkdir /home/git/Hello-World.git

Notice the .git suffix in the folder’s name. This suffix is not mandatory, and is just a convention: on the server side, repositories have this suffix. It’s a convention you can find on GitHub, for instance.

Now change your current directory to this new one:

cd /home/git/Hello-World.git

Finally, we initialize the repository:

git init --bare

The --bare option is here to indicate that we want to create a bare repository on the Raspberry Pi. Once again, it’s a convention: a bare repo doesn’t store the data the same way they are stored on a working repo. Bare repositories are not adapted to be working repositories, but they are perfect for a server. More information about bare repositories can be found in Git’s documentation.

Initializing the repo on your computer

On the computer you’ll use to work on your project, create a new folder (anywhere you want). In a terminal, go to this directory (with cd) and initialize the repo with the following command:

git init

Here we create a normal repository and not a bare one. That way, we’ll see files the way we expect to on our computer.

Our two empty repos need to communicate. To enable this communication, we’ll create a remote on the working repository. Still in the working repository’s folder on your computer (where you launched git init), type the following command:

git remote add pi git@XXX.XXX.XXX.XXX:/home/git/Hello-World.git

This command creates a new remote named pi (you can choose whatever name you want). The XXX.XXX.XXX.XXX must be replaced by the IP address of the Raspberry Pi (local or not). Finally, the /home/git/Hello-World.git part refers to the absolute path to the repository on the server, so think about adapting it!

Working with the repositories

Now our repo is ready to use. Create new files, edit them, remove some others, as always. When you’re ready to create your first commit, just do the usual:

git commit -m 'My first commit'

After a few commits, you’ll want to send your changes to your server. To do that, use the following command, which should sound familiar to you if you’ve already used something like GitHub:

git push pi master

In the above command, we send the master branch to the remote repository named pi. It’s the remote we created in the previous part, so think about changing its name if you chose another one!

Now, someone else with access to the server — or you on another computer — can download your changes and make commits. To download the latest changes and get an updated working repository on your computer, type the following command:

git pull pi master

Cloning a repository

Bringing in a new user? Or adding yourself on another computer? The repository exists on the server, but it needs to be added to another computer. This is when it’s time to clone the repository.

Cloning a repo requires knowledge of where it lives — or, in other words, what its address is. As it’s your server, you should know it, and it’s the same as the one we used above:

git clone git@XXX.XXX.XXX.XXX:/home/git/Hello-World.git

By cloning a repository, a remote is automatically created, named origin, so you don’t have to create yours as we did above.

Closing Words

As said above, the server we just built is very simple, but it may well be enough for small or private projects. If you need more features, you should consider another tool, or something like a private GitHub repository.

Have you set up a Raspberry Pi as a Git server? Or done something else interesting with a Pi? Please tell us about it in the comments.

Frequently Asked Questions (FAQs) about Setting Up Your Raspberry Pi as a Git Server

What are the prerequisites for setting up a Raspberry Pi as a Git Server?

Before you start setting up your Raspberry Pi as a Git Server, you need to have a few things in place. First, you need a Raspberry Pi with an installed and updated version of Raspbian. You also need a stable internet connection for your Raspberry Pi. It’s also important to have a basic understanding of Linux commands as you’ll be using the terminal for the setup process. Lastly, you need to have Git installed on your Raspberry Pi. If it’s not installed, you can do so by running the command sudo apt-get install git-core.

How can I secure my Git server on Raspberry Pi?

Securing your Git server is crucial to protect your data. You can secure your Git server on Raspberry Pi by changing the default password of your Raspberry Pi, keeping your system updated, and configuring firewall rules. You can also disable root login and set up SSH keys for authentication instead of using passwords. Regularly backing up your data is also a good practice to prevent data loss.

Can I access my Git server remotely?

Yes, you can access your Git server remotely. Once you have set up your Git server on Raspberry Pi, you can access it from any device with Git installed. You just need to use the IP address of your Raspberry Pi and the name of the repository you want to access. For example, you can clone a repository using the command git clone pi@your.pi.ip.address:/path/to/your/repository.

How can I create a new repository on my Git server?

Creating a new repository on your Git server is simple. First, navigate to the directory where you want to create your repository using the cd command. Then, create a new directory for your repository using the mkdir command. Finally, navigate into your new directory and initialize it as a Git repository using the git init --bare command.

How can I manage multiple repositories on my Git server?

Managing multiple repositories on your Git server is straightforward. Each repository is just a directory on your Raspberry Pi. You can create, delete, and move repositories just like any other directory. To clone a specific repository, you just need to specify its path when running the git clone command.

Can I use my Git server with a GUI client?

Yes, you can use your Git server with a GUI client. Most Git clients allow you to add remote repositories by providing their URL. In this case, the URL would be the IP address of your Raspberry Pi and the path to the repository.

How can I troubleshoot issues with my Git server?

Troubleshooting issues with your Git server can involve several steps. First, check the error message you’re getting as it often provides clues about the problem. If you’re having network issues, check your Raspberry Pi’s internet connection and its firewall rules. If you’re having issues with a specific repository, check its permissions and whether it’s properly initialized as a Git repository.

How can I update my Git server?

Updating your Git server involves updating Git and the operating system of your Raspberry Pi. You can update Git by running the command sudo apt-get install git-core. You can update your Raspberry Pi’s operating system by running the commands sudo apt-get update and sudo apt-get upgrade.

Can I set up a Git server on a Raspberry Pi Zero?

Yes, you can set up a Git server on a Raspberry Pi Zero. The process is the same as setting up a Git server on any other Raspberry Pi. However, keep in mind that the Raspberry Pi Zero has less processing power and memory, so it might not perform as well with larger repositories or multiple users.

How can I back up my repositories?

Backing up your repositories is crucial to prevent data loss. You can back up your repositories by copying their directories to another location. You can do this manually or set up a script to do it automatically at regular intervals. You can also use Git’s built-in features to clone your repositories to another location as a backup.

Jérémy HeleineJérémy Heleine
View Author

Currently a math student, Jérémy is a passionate guy who is interested in many fields, particularly in the high tech world for which he covers the news everyday on some blogs, and web development which takes much of his free time. He loves learning new things and sharing his knowledge with others.

Emerging TechgitRalphMRaspberry Piserver
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week