Coding truck

How to make a local git server with a Raspberry Pi

Ever had a git repository that you wanted to copy to a remote server? Github is a great choice. It has a great interface and it's free for your open source projects.

Your repository isn't an open source project? Well that's fine. Bitbucket is a great choice for private repositories. It's free and it has a great interface as well.

  • You want to save some really large files?
  • You don't want to push it into the cloud?
  • You want it to be part of your IoT smart home project?

We've still got you covered! You can setup a secure remote git repo and access it with ssh. Even better you can use a Raspberry Pi to host your own server in your own home.

Setup is easy. Let's walk through the steps.

Get started!

The Raspberry Pi is a great single board PC that has the incredible price point of $35. When you throw in a case, power supply and SD card it will cost around $60.

Once you have the hardware, you will need to install the Raspbian OS and configure your pi. We have outlined these steps in other posts

Make the remote git repo

Now that you have your pi setup, it's time to create the remote repository that your pi will use to store a copy of your local repository.

To get started, connect to the raspberry pi server

ssh j@muse

I like to make a folder for my git repositories. I will make one with the mkdir command. Since I like to keep things simple I will name the folder 'git' and place it in my home directory.

mkdir git

I'm making a git repository called carnitas for a project I'm making. I'm going to make a git repo for it. First I will go to the git folder that I made

cd git

Now, I will make a folder for the remote git repository. I will name it carnitas.git to easily identify that it is the carnitas git repository.

mkdir carnitas.git

Now I'll go into the carnitas.git folder and initialize it as a git repository. Because it is a remote repository, I will use the --bare option

cd carnitas.git
git init --bare

That's it! The remote repository is all setup. Now let's close the connection to the raspberry pi.

exit

The next step is to setup our local git repo on our workstation.

Make the local repository

We have now exited our remote connection and will cd to the location that we want to have the local repo.

cd ~/git

Make the local repository, this time without the --bare option

mkdir carnitas
cd carnitas
git init

Finally, let's add the remote link. This will connect the local repository to the one on the raspberry pi via SSH.

git remote add origin ssh://j@muse/~/git/carnitas.git

Now let's test it

touch README.md
git add .
git commit -m "Added README"
git push origin master

If all goes well you should see something like:

Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 222 bytes | 222.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://muse/~/git/keyword-finder.gits
 * [new branch]      master -> master

Cloning from another computer

If you want to clone the remote repository on the raspberry pi from another computer that is easy to do as well. Simply use:

git clone ssh://j@muse/~/git/carnitas.git carnitas
Next
Prev