While Github is a great way of sharing your project and cooperating with others, often times you would like to have stuff such as your code or configuration files under version control.
You can setup a Gitlab service at home if you want to be in control of your central repository, but unless you are going to be colaborating with a team it can be an overkill and somewhat time consuming to install and configure.
I like having my private config files and scripts at home in my Raspberry Pi, so that I can synchronize my configurations, aliases and scripts between my desktop, laptop, other raspberries and my PC at work. Github allows private repos, but why would I share details about my systems, users and networks with a third party?
Once you have this setup in place, you can even automate backups of your configuration using etckeeper.
edit: you also need a runinng SSH daemon
Setting up a private git repository is almost too simple.
- Log in into your Raspberry Pi.
- Install the git package
- Create a git user
sudo adduser --gecos "" git
That’s it! You can now create a bare repository in your Pi, owned by the git user
mkdir -p /media/USBdrive/git/myrepo.git sudo chown -R git:git /media/USBdrive/git cd /media/USBdrive/git/myrepo.git git init --bare
And access it from outside. Say your hostname, IP or domain for your Raspberry Pi is server
git clone ssh://git@server/media/USBdrive/git/myrepo.git
Of course this is a bit too simple. We want a little extra security.
If you haven’t already, generate the SSH keys for the device that you want to access the repo
ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa
Register your public key with the git repository
, which is equivalent to
cat ~/.ssh/id_rsa.pub | ssh git@server "cat >> ~/.ssh/authorized_keys"
Disable login sessions and password for git user, so only users in authorized_keys can access
sudo passwd -l git
Finally, use git-shell for the user git as a limited shell for git operations only.
sudo usermod -s /usr/bin/git-shell git
That’s it! You now have a secure, self-hosted git repository. Easy!