The Raspberry Pi made it quickly to many programmers home, and is used for various purposes. I have one running as scan server, where the multifunction printer stores the scanned PDF files to be accessed from every device on the home network. Since I do a lot of pet project coding and want these projects under a source control, I decided to set up another Raspberry Pi as Git server.
First of all, you need a Raspberry Pi which is set up with the latest version of the Raspbian OS. You need to install Git and SSH on the Pi, therefore an Internet connection is needed, too.
To install Git, run the command
sudo apt-get install wget git-core
This downloads and installs the latest Git version onto your Pi. Afterwards run
sudo apt-get install ssh
to install SSH support. After the installation, you theoretically need to start SSH after every boot of your Pi. Because this is cumbersome and nothing really pleasant, you need to automatically start SSH after every boot. You can do this by running
sudo update-rc.d ssh defaults
If you now run
your Pi reboots itself and automatically starts SSH on boot.
Set a hostname
To comfortably access your Git Server setting an easy to remember host name is recommended. Open /etc/hostnamce to set a custom host name for your Pi:
sudo leafpad /etc/hostname
Type the host name you want to use and save the file - I picked gitserver1 as host name for my Pi.
Afterwards, replace every occurrence of raspberrypi in /etc/hosts with gitserver1 (or whatever name you decided to give). Edit the file by running
sudo leafpad /etc/hosts
After you have done that, reboot your Pi by running
Add a Git user and user group
To use Git from remote, you need a user and a user group for it:
sudo adduser --system --shell /bin/bash -gecos 'Git user for Marco Bertschi' --group --home /home/marco marco
This creates a user named 'Marco'. You need a password for that user, therefore run
sudo passwd marco
You will be prompted to set a password.
After you have set the new password you can switch to the newly created user and initialize the first, empty git Repository:
su marco #Switch to user marco
cd /home/marco #go to home drive where user has full read/write privileges
mkdir myrepository.git #create an empty repository
cd myrepository.git # go to newly created repo
git --bare init #initialize the empty repository
Push your code to the Pi
After the Pi is set up as Git server you can push code to the previously created repository on the Pi. You can do this by creating a local repository and then pushing it to the Git Pi:
git remote add origin marco@gitserver1:/home/git/myrepository.git
git add -A
git commit -m "Initial commit"
git push origin master
Congratulations! You just have created a Git server on your Raspberry Pi and pushed code to your first repository.
Points of Interest
Git (and especially the Git Bash) can be very confusing topics for beginners - I hope that I was able to clear the dust a bit, and give beginners a good guide on how they can handle Git, but also made a Reference for experienced Git users who need to refresh their knowledge, for whatever reason it may be.
If you haven't found what you are looking for after reading this article, or maybe need some further information feel free to go through the links I provided in the chapter "Further Reading" below.
This chapter provides links to some sources which helped me writing this article - Git Infos are not easy to find, especially if you are looking for a different explanation which doesn't just say "type XYZ". I wanted some special sources sources which provide an overview and/or explain the background behind certain commands, or even give you Tips, Tricks, and Tweaks. Here they are:
Basic Git Command Line References for Windows Users - John Atten
A few Git tips you didn't know about - Mislav Mahronic
--> First publication