KevCaz's Website

As mentioned in my previous post, I have recently started using my Raspberry Pi as a server and I decided to try to host a basic Git server.

As explained in the chapter “Git on the Server” of Pro Git1, among other protocols, Git supports SSH and it is quite straightforward to setup a basic Git server via this protocol. As I can access my Raspberry Pi via ssh, I decided to give it a try! I first connected to it as user pi (ssh pi@raspberry.local), a user that is listed as a super user, and I created a new user: git (this is one way suggested in Pro Git).

1
2
3
4
# add user 
$ sudo useradd git
# set its password 
$ sudo passwd git 

Then I created the directory git in /srv (see the FHS for this choice)

1
$ sudo mkdir /srv/git

and made the user git its owner.

1
$ sudo chown git /srv/git/

Then I logged out and logged back in as user git (ssh git@raspberry.local) to create a bare repository demogit.git (note that the final .git is a mere convention).

1
2
3
4
$ mkdir /srv/git/demogit.git
$ cd /srv/git/demogit.git
$ git init --bare --shared
Initialized empty shared Git repository in /srv/git/demogit.git

Well, that was basically it, demogit.git was now ready to be the remote repo for one of a local repo! So I logged out and went back on my computer where I created a local repo and a first commit.

1
2
3
4
5
6
7
8
# create the git repo 
$ mkdir ~/demogit
$ cd demogit
$ git init
# create a first commit
$ echo "# demogit" > README.md
$ git add README.md
$ git commit -m "add README"

Then I added my remote repo.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# add remote 
$ git remote add origin git@raspberrypi.local:/srv/git/demogit.git
# set upstream branch 
$ git push -u origin main
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 880 bytes | 880.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To raspberrypi.local:/srv/git/demogit.git
 * [new branch]      main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.

🎉 Now I have a local repo on my main computer and a remote repo on my Raspberry Pi. So yes, setting a basic (and really remote2) Git server could be as simple as setting a bare Git repository in on a computer to which you have ssh access to, which is pretty neat!


  1. Pro Git (2014), S. Chacon & B. Straub. https://git-scm.com/book↩︎

  2. You can actually use your own computer, with file:///path/to/bare-repo.git as remote repository. ↩︎