Git Server for Raspberry Pi

Image for 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.

Previous Step

  Git Client for Raspberry Pi

What you will need

(Or Larger)

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:

git --version

![Git Version}(http://

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:

![Git Version}(http://

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 pi@

Seemingly, nothing will happen. That's good:

![Git Version}(http://

Use this command to see what remote repository a directory is connected to:

git remote

![Git Remote}(http://

We're all connected!

Create a File and Add to Repo

Next, create a file for the repo by typing this:

echo "This is my repo" >

![Git Remote}(http://

Add it to the repo with this command:

git add

No news is good news. Next, commit your new file to the server with a message of Initial Commit:

git commit -m "Initial Commit"

![Git Remote}(http://

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 yes.

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.

![Git Remote}(http://

Now, we've successfully uploaded:

![Git Remote}(http://

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 pi@

It should have created a directory called MyRepo, so switch to that directory and dir the contents:

cd MyRepo dir

You should see the file that we created:

![Git Remote}(http://

Next Up

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!

Be sure to subscribe to our channel!