How to run a git server on your local machine
Let’ see how you can install a git server locally on your development machine.
I see a couple of reasons why would you want to do that in the first place:
- to have a safe playground to play with git
- to see what is happening on the server when you execute git commands (what’s what I did with the couple of git related articles on this blog)
- and finally, why not?
Git in a docker container
The easiest way to run a git server is to run it inside a docker container.
There is a pretty popular Docker container to run git: jkarlos/git-server-docker
This container exposes the port 22
and 2 volumes:
*/git-server/keys*
: Volume to store the users public keys*/git-server/repos*
: Volume to store the repositories
Directories setup
One simple way to set up this git image is to have a dedicated directory for the server data like: $HOME/git-server
In that directory, you can create 2 subdirectories:
keys
to store the SSK key(s)repos
to host the git repositories directories
We will use the --volume
to share those directories with the docker container.
SSH keys
Step #1 is to create the ssh keys: even though the server will be running locally on your machine, you still need ssh keys to allow the SSL connection.
The command to create the key is the usual ssh-keygen
. For the sake of clarity, you may want to save the key in a different file:
In order for the container to access the public key, you need to copy it to your keys
directory
Starting the git docker container
We can start the server by setting the volumes and we will be using the port 2222 locally.
You can test that the server is properly running by trying to ssh connect to it, using your private key:
Network setup
In order to make it easy to access this git server from the command line, you can configure your ~/.ssh/config
file to configure a pseudo remote host which will connect to your docker container:
Create a bare repository
In order to create a repository on the server, you must create a so-called bare repository (the directory structure is not quite the same as the structure on the client side, mostly because there is no such thing as a working directory there).
You can then clone that repository, in a different directory (obviously), with the command:
and now, you are in business!