Git Server for Raspberry Pi
Tinkerer: Lory Livezey
In the last tuturial, we learned how to download libraries from GitHub to the Raspberry Pi using the Git command. In this tutorial, we'll learn how to set up the Raspberry Pi as a server. This will allow you to store libraries on your Raspberry Pi so that they can be retrieved from another Pi or any computer running Git.
What you will need
Installing Git on Raspberry Pi
If you didn't install
git previously, or if this is a different pi, then run:
sudo apt-get install git
Once installed, test it by getting the version:
Create a Library
Create a directory where you would like to install your repositories. If you follow my tutorials, you may want to keep it here so that the commands will work exactly:
Go to that directory, and create a directory for your new repo:
cd /home/pi/TinkerPi/git mkdir MyRepo.git cd MyRepo.git
Next, initialize the directory.
git init --bare
If you do an
ls, you will see the files that were created. Your screen should look like this:
Connect to Library from another Device
From your computer or another Raspberry Pi, go to a directory where you would like to create the repo. Then run the following command, replacing the IP address with your own.
gitpi is a name that we are assigning to refer to this server in the future.
git remote add gitpi firstname.lastname@example.org:/home/pi/TinkerPi/git/MyRepo.git
Seemingly, nothing will happen. That's good:
Use this command to see what remote repository a directory is connected to:
We're all connected!
Create a File and Add to Repo
Next, create a README.md file for the repo by typing this:
echo "This is my repo" > README.md dir
Add it to the repo with this command:
git add README.md
No news is good news. Next, commit your new file to the server with a
git commit -m "Initial Commit"
So far, everything that's happened has been on the
local computer. That's because git does a lot of it's magic in a local repository first, then it syncs that with the remote repository. The next command will
push the changes to the remote repo:
git push gitpi
gitpi is not a command, but the name of our remote server that we made when we first connected to it.
You should see this message about the
authenticity of the remote host. Don't worry, we know who it is because it's US! Say
You may get an error as I did, saying you don't have sufficient rights. If you do, just type the
git push gitpi command again. It needs your password, but didn't get around to asking because it got distracted with the authenticity message.
Now, we've successfully uploaded:
Test the Repo By Cloning It
Now for the test.. We're going to go to anther directory and clone our repo back down:
git clone email@example.com:/home/pi/TinkerPi/git/MyRepo.git
It should have created a directory called
MyRepo, so switch to that directory and
dir the contents:
cd MyRepo dir
You should see the
README.md file that we created:
Git is a very powerful tool, and we have just scratched the surface here. But I believe in
learn as you go, so that's what we're going to do. We'll learn more advanced features of Git as we go. See you soon!