September 4, 2014

How to set up a private Git server on a Raspberry Pi (or any Linux-based OS, really)

Paying for private Git space sucks. And sometimes some code is really not worth putting out onto the greater web, or really doesn't need to be up there. The solution is to privately host your repository!

A Raspberry Pi is a super cheap, small, power-effective computer, excellently re purposed as a Git server. Start by installing some dependencies - chances are, if you're running Raspbian, you may already have these.
sudo apt-get install wget git-core

It is good practice to set up a dedicated system user, in this case we are calling the user git with a home folder location at /home/git
sudo adduser --system --shell /bin/bash -gecos 'Raspberry Pi Git user' --group --home /home/git git

Enter the user's home folder, and initialize a new bare git repository:
su git
cd /home/git
mkdir newrepo.git
cd newrepo.git
git --bare init

And your Pi is now set up with a shiny new repository, ready to accept some files!

Return to your host machine, and go to the root of the new project that is about to get pushed to the new repository. At this point, it is a good idea to check your .gitignore file to make sure you aren't about to push stuff that shouldn't be version-controlled. Replace the IP address below as needed; or better yet, hostname if that is set up (mine is just running on my local network). We will now tell our host machine where to push to, then will commit the project, and actually push it:
git init
git remote add origin git@192.168.0.xx:/home/git/newrepo.git
git add .
git commit -m "First commit"
git push origin master

If, like me, you already had a git work tree set up, but the old repository is now defunct, you can instead just change the repo location like this:
git remote set-url origin git@192.168.0.xx:/home/git/newrepo.git
git push origin master

This will retain your entire git history, even though you may previously have been pushing to a different repository. If you had the entire history checked out locally before your switchover, that history will be uploaded to the new location as well.

Finally, as a last quality-of-life tweak, upload your public SSH key to your remote user's ~/.ssh/authorized_keys file. This stops the annoying password prompts during every push, but is just as secure.

Lastly, back on the host machine, it might be worth setting Git's case-sensitivity off via git config core.ignorecase false.

No comments :

Post a Comment