Creating a private git repository on a private server

Tutorial about how to create a private git repository on a private server

Mad Cat

4 minute read

Cat playing

Introduction

Many cats know and use github for storing their git repositories, but sometimes you have customers whose requirements don’t allow them to use any public git service. Other times you don’t want to let the world know about your messy coding style, or how do you comb your whiskers or perhaps you are only making experiments.

For all these use cases and many others, you can always create a private git repository. Of course you need to have access to a private server with the required software, note that any common GNU/Linux modern distribution will allow to do this in an easy manner.

How can we create a private repository then? These are the steps to create it.

Step 0. Creating a git repository

This initial step has to be followed in the case you are starting from scratch a new project or just if you want to test this tutorial and you don’t want to mess with your other repos.

For creating a new repo execute the following commands from a shell:

$ mkdir newproject
$ cd newproject
$ touch README.TXT
$ git init 

Step 1. Setting up git parameters for clients

The first mandatory step (If necessary) is to configure the following parameters in git, using the following commands:

$ git config --global user.email "[email protected]"
$ git config --global user.name "example" 

Of course, you should replace the strings with your own email address and name.

Step 2. Setting up the git user

In the next step we must check if the git user and group has been created in the server. Let’s say that the server is located in example.com. First of all we connect to the server via ssh and check if the group already exists. If the group doesn’t exists we can create it with the following command (as root):

# groupadd git

And for creating the user (If needed) we must use the following command (as root):

# useradd -m -g git -d /var/git -s /bin/bash git

Note that -d specifies the home directory for the user, hence your repos will be stored in the specified folder. Probably there are better options for the shell that /bin/bash but we are going to secure the access with SSH Keys in the following step.

Step 3. SSH Keys

SSH uses public key cryptography for establishing the connections between the server and the client. For creating a keypair and copying the public key to the server we must execute the following command in the client machine:

$ ssh-keygen

After that we will have a new keypair in the .ssh directory. Note that if a keypair already exists there is no need to create another one.

Then we must copy the content of the public key to the file /var/git/.ssh/authorized_keys on the server. We can create different keypairs for each client, and revoke access to an user on the server by deleting the corresponding public key in the authorized_keys file.

$ ssh-copy-id [email protected]

For connecting we are going to need the private part of the keypair. (Like in having the file with the private key in the device that we are going to use to connect to the server).

Step 4. Creating a bare repo on the server

Connect to your server and become the git user:

# su git
$ cd

Then we create the bare repo with:

$ mkdir /var/git/newproject.git
$ cd /var/git/newproject.git
$ git init --bare 

After this step our repo is accesible using the private ssh key, with the following URL [email protected]:/var/git/newproject.git .

Step 5. Pushing and existing project to the private bare repo

In case we started a new project from scratch we can commit some changes localy (In the client machine):

$ touch test
$ git add test
$ git commit -m 'initial commit'

Finally we add the remote to the repo and push our local repo to the server:

$ git remote add origin [email protected]:/var/git/newproject.git
$ git push origin master

With this last step the repo on the server will receive all the files and changes and we can use the repo in the server to get the code in any other machine.

And that’s it, let’s celebrate our achievement playing CATeroids.

comments powered by Disqus