raspberry-git
Table of Contents
I've been using GitHub for years to backup projects or share code. But the recent push towards LLMs makes me hesitant to share my code on GitHub because I don't want it to be used as training data. So, I set up my own git server running on a raspberry pi. It's really easy and you can find out how to do it below.
1. New User
First, we're going to set up a new user on the raspberry pi to store repos. Technically, you don't have to do this. You can use an existing user but this will let us use separate authentication methods when accessing the pi for git and accessing the pi for anything else.
Run the following on the raspberry pi:
sudo useradd -s /bin/bash -m git sudo passwd git
2. The Key Dance
We will use keys for authentication over ssh. Run the following locally to create a new pair of public and private keys:
cd ~/.ssh
ssh-keygen
ssh-keygen
will ask you for the file to create and a password. If you don't want to use a password to log in just press enter when prompted for one.
Next, we need to share our keys with the raspberry pi. Run the following locally. PUB_KEY
is the file you just created that ends with .pub. LOGIN_INFO
if made from the user you created in the first step and the IP address of your raspberry pi. You can find the IP of your raspberry pi by running ifconfig
(it's on the line that starts with inet).
PUB_KEY=mykey.pub LOGIN_INFO=foo@1.2.3.4 cd ~/.ssh ssh-copy-id -i $PUB_KEY $LOGIN_INFO
This will add the key to the authorized keys lists on the raspberry pi. You can test that it's working by sshing into the raspberry pi:
LOGIN_INFO=foo@1.2.3.4 ssh $LOGIN_INFO
3. Create a Repo
Next, we need to create a new repo on the raspberry pi. Run the following on the raspberry pi:
PROJECT=my_project REPO=$my_project.git mkdir -p $REPO cd $REPO git init --bare
This will create and initialize a bare repo that we can push code to.
4. Push
Now we are ready to use the repo! Run the following on your local machine:
LOCAL_REPO=/path/to/my_project REMOTE_REPO=my_project.git REMOTE_USER=foo LOGIN_INFO=$REMOTE_USER@1.2.3.4 cd $LOCAL_REPO git remote add origin ssh://$LOGIN_INFO/home/$REMOTE_USER/$REMOTE_REPO git push -u ssh://$LOGIN_INFO/home/$REMOTE_USER/$REMOTE_REPO
Congratulations, you now have your own git server that you can push and pull code to :)
5. Troubleshooting
SSH and keys can be finicky to setup. A common issue is having the wrong permissions on your keys Here is a great stackoverflow answer for this problem. The TLDR is that your keys on your local machine need to be readable and writable only to you. You can set this by running
chmod 600 path/to/keys