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.
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.
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 is because the Pi is unable to provide enough power via the USB port to keep an external drive powered.
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.
- 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
- 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.
- 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 on mounting a USB drive on Raspberry Pi incredibly handy.
This 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.
- Firstly, we need to make 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
- Now let’s move into the directory by using the cd command.
- Now let’s initialise the git repository using the bare command.
git init --bare
- 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.
- On a 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.
- Once you have installed it you should have some extra options when you right click in a folder.
- In the folder you wish to commit, right click and then go to git bash. This should open a window that looks like a command prompt.
- 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:
- Now we need to add our remote Git directory by adding the following line.
git remote add myFirstRepository firstname.lastname@example.org:/home/pi/git/myFirstRepository
- Now we can easily push to data to our repository. To do this enter the following commands.git add .
git commit -m "Our first commit" git push myFirstRepository master
- It should come up with a success message. This means our code has been pushed to our git server.
- To just test to see if everything is working correctly you can clone the repository we just setup to a new folder. First open up git bash in a different folder and then run the following commands:
git clone email@example.com:/home/pi/git/myFirstRepository/
- As you can see the Raspberry Pi git server is 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 with 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 on 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.