I recently started on a software collaboration with another developer. Given the distance between us and the need to work remotely, we made the choice to use git for source control.
Since our project could eventually have commercial applications we wanted some privacy, so at first we first looked at setting up a "hidden" repository on Github. That however requires a monthly membership fee and we are on a low budget.
After briefly considering a few other options (a git repo synced via Dropbox or SpiderOak for example) I had a flash of sudden inspiration. Why not turn one of my three Raspberry Pis to host a git repository?
In an impressive tribute to the Raspberry Pi's versatility my partner and I accomplished this in less than 10 minutes!
There are basically three steps to setup your Raspberry Pi so it can be used as a central git repository:
- Assign a static IP address to the Raspberry Pi
- Port forward your Pi's SSH port
- Setup a git repo and connect to it remotely
Assigning a static IP address is fairly straightforward. You need to modify your network interfaces file:
sudo vi /etc/network/interfaces
You should see a line that says:
iface eth0 inet dhcp
This is the configuration for your Raspberry Pi's Ethernet port. By default it is configured to use the DHCP service on your Router to automatically assign it an unused IP address on your home network. For most cases this is what you want. After all, who wants to manually assign IP addresses to every device on their network? However, the problem with using DHCP is that you cannot depend on your Raspberry Pi always having the same IP address. This makes it hard to connect via SSH and so forth.
The first step is to reconfigure your Router's DHCP service to reserve a block of IP addresses that you can assign yourself. By default most routers are configured to assign IP address from the
192.168.1.255 range (with
192.168.1.1 reserved for the router itself). You want to shrink this range to leave you a few IP addresses at either end that you can assign yourself.
Instructions are different for every router. Usually you connect to
http://192.168.1.1/ and then log into your router using your router's administrative user name and password. Find your network's DHCP settings. These are often buried in the Advanced Settings page for your network.
Somewhere in those settings it should define the start and end of the range of IP addresses that DHCP will assign. Modify either the start or the end to reserve some IP address for yourself. (My home configuration is that DHCP assigns
192.168.128 leaving the range of
192.168.1.255 to assign statically as I choose.)
Once that is done, you can select an IP for your Raspberry Pi. Personally, I keep a list on my computer of static IP address and what I have assigned them to on my network so I do not accidentally create an IP address conflict. A little bit of bookkeeping can save you a lot of headache later!
Edit the interfaces file again:
sudo vi /etc/network/interfaces
iface eth0 inet dhcp
iface eth0 inet static address 192.168.1.<STATIC IP ADDRESS> netmask 255.255.255.0 gateway 192.168.1.1
Note: If you have a different gateway IP (which is the IP of your router) or a different netmask, substitute those values accordingly. What I list are the common defaults.
Reboot your Raspberry Pi. It should now be using its static IP!
The Raspberry Pi should have SSH installed by default. If you have not enabled SSH on your Raspberry Pi you can do so by running:
And choosing Advanced Options -> SSH and setting it to Enable. Reboot your Raspberry Pi.
At this point you should be able to use an SSH client (such as PuTTY) to connect to your Raspberry Pi from within your home network. Verify you can connect using its static IP and the "pi" user name / password.
At this point your Raspberry Pi could be used to host a git repository within your home network. That may be enough for some folks, so you can skip to the next section if you so choose.
If you want to able to connect to your git repository from the internet, you will need your router to forward your SSH port. This exposes that port on your router and allows outside connections to access your Raspberry Pi from the internet.
You willl need to log into your router once more. As before, each router is going to be different, but most have a Port Forwarding settings page or something like that. You will want to enter or select your Raspberry Pi's IP address as the source IP and your SSH port as the port to forward. SSH defaults to port 22, but you may decided to change it by editing the
/etc/ssh/sshd_config file and replacing the Port 22 line with a different port number.
While you are logged into your router, make note of the router's external IP address (this is not your Gateway IP which typically starts with
192.168, this is the IP your ISP has assigned you.)
Once the port forwarding rule is setup, you should be able to connect to your Raspberry Pi from the internet. Open up an SSH client (such as PuTTY) and connect using your external IP address and your "pi" user name / password. If you can successfully connect, then you are done!
Note: If you cannot readily find your external IP address you can visit: http://www.whatismyip.com and it will display it for you.
All the connection infrastructure should be set up for you to connect to you Raspberry Pi from inside or outside your network. Now all you need to do is set up a git repository!
Log into your Raspberry Pi. Create the directory where you want your git repository to reside (/opt/git/sandbox/ for example). Create a git repository there:
git init git checkout -b EMPTY
Note: By default git creates a "master" branch. Most development paradigms however actually use the master branch as part of their workflow. In git you cannot push to a branch that is checked out upstream. The workaround is to create a dummy branch (the EMPTY branch above) which remains checked out. This leaves the master branch free to be pushed to from clients downstream.
Now go to your development machine. You can clone this repository via:
git clone ssh://pi@<IP ADDRESS>:<SSH PORT>/<YOUR PATH> .
git clone ssh:[email protected]:22/opt/git/sandbox/ .
If you are going to be accessing this repository away from your home network then
<IP ADDRESS> should be your external IP address (see Port Forwarding section above). If you are only going to be accessing this repository on your internal home network then
<IP ADDRESS> should be the static ip address you assigned your Raspberry Pi.
Questions? Comments? Email me at: [email protected]