In this tutorial, we make a simple yet cool Raspberry Pi Git server that is perfect for hosting your next code project.
If you’re a programmer, then you probably have heard of Git before. For those who haven’t heard of it, Git is a hugely popular version control software for the development of software.
There are plenty of hosted Git solutions out there, but you may be wanting your own private Git server for one reason or another. Such as having full control over your code and being able to keep it as private as possible.
One good benefit of learning how to run your own Git Server is that you gain an understanding of a lot of the Git commands. Knowing these commands is extremely handy, especially in jobs where they don’t use a GIT graphical interface.
You will need the following equipment to get this private Git server up and going.
Note: If you plan on using an external hard drive than it is likely you will need a powered USB hub. This requirement is because the Pi is unable to provide enough power via the USB port to keep an external drive powered.
If you want to see how this is all done, then you can watch my video below. It will take you through all the steps to getting your very own Raspberry Pi GIT server up and going.
If you like the video, then please make sure you subscribe, so you stay up to date on all our future projects.
Adblock blocking the video? Support us by subscribing to our ad-free service.
Installing the Raspberry Pi Git Server
The process of installing the Raspberry pi Git server is pretty straight forward. If you’re not familiar with Git and its commands, then I highly recommended that you take the time to learn them. I will go into a few basics further down the page and also point out a few good places to learn more about using Git.
1. As always let’s first make sure that the Pi is up to date. Run the following commands
sudo apt-get update sudo apt-get upgrade
Firstly, make sure Git is installed. (It should already be installed)
sudo apt-get install git-core
2. secondly you will need to make sure you have SSH enabled. If you haven’t got this done, then you can find out how to do this in my SSH tutorial.
3. Now you might want to store this on a USB drive. If you want to do this, then you will need to mount the USB drive. You will find my guide to mounting a USB drive on Raspberry Pi incredibly handy.
This process is all you need to do to have the Git server up and running. It really is pretty easy, the next part we will go into creating your first repository and pushing updates to it.
Setting up your First Repository
Now let’s make our very own first repository so that we can start pushing code/data to it. There are more complex commands that you will probably want to learn, but these basics will be perfect to get you going.
1. Firstly, we need to make a directory for where our new repository will be stored. The -p tag will create any directories in our path that doesn’t already exist.
mkdir -p /home/pi/git/myFirstRepository
2. Now let’s move into the directory by using the cd command.
3. Now let’s initialize the Git repository using the bare command.
git init --bare
4. You will need to repeat these steps whenever you need to make a new repository. Now that’s all done we’re ready to do our first commit.
5. On Windows or Mac computer you will need to download Git to be able to start pushing code to our repository. I recommend downloading and using git-scm.
6. Once you have installed it, you should have some extra options when you right click within a folder.
7. In the folder you wish to commit, right click and then go to Git bash. It should open a window that looks like a command prompt.
8. If this is not already a Git directory, then you will need to initialize it before we can push the code to our Pi. To do this enter the following command:
9. Now we need to add our remote Git directory by adding the following line.
git remote add myFirstRepository email@example.com:/home/pi/git/myFirstRepository
10. Now we can easily push data to our repository. To do this enter the following commands.
git add . git commit -m "Our first commit" git push myFirstRepository master
11. It should come up with a success message. This message means our code has been pushed to our Git server.
12. To test to see if everything is working correctly, you can clone the repository we just set up to a new folder. First, open up Git bash in a different folder and then run the following commands:
git clone firstname.lastname@example.org:/home/pi/git/myFirstRepository/
13. As you can see, the Raspberry Pi Git server is now storing our code correctly. Now, this is the very basics of Git, and there is so much more to learn.
Now there is a lot more to learn about Git and the commands you’re able to use. I have come across a couple of websites that have been incredibly helpful in learning more about Git and how it works. If you’re looking at getting into programming, then it is important that you learn about code version control systems.
If you want to learn more about Git, codecademy has some fantastic tutorials that you can check out.
Also, Git-scm has plenty of great documentation on the many different commands you can use.
Accessing the Private Git Server Remotely
Now you may want to access your Git repositories outside your local network. To do this, you will need to setup port forwarding on your router.
There are a few security risks involved with allowing communication through a port but is the easiest way for gaining access to your server.
It is essential that you update the pi user’s password to something a lot stronger if you’re going to allow external access.
If you want to setup external access, then check out my guide on port forwarding with the Raspberry Pi. You will need to port forward the SSH port (22) to gain access outside your local network.
If you use an external port other than 22, then you will need to specify this either in ~/.shh/ssh_config or using a URL style address instead like as followed.
Where X represents your external IP and 23 is the external port.
I hope that you now have a fully working Raspberry Pi Git server. If you come across any problems, I have missed something or anything else then be sure to drop me a comment below. If you like my projects, then be sure to give me a follow over on any of the major social networks.