Set up a private git server

Published by Alexander Braun on 18 Dec 2017 - tagged with Linux, Security

In this post we are going to install a private git server, that can be used for small to medium size personal projects. The described process works on any Red Hat based distribution, e.g. CentOS or Fedora, but it should be easy to implement it on other distributions as well.

Introduction

Setting up a private git server for your personal projects is quite easy to achieve. I personally use a private git server all the time as it has a lot of benefits for the development process:

  • Sharing code between different computers (e.g. notebook and desktop)
  • Having the ability to roll back easily in case of errors
  • Distributing work across different persons
  • Using git as backup in case of a hard drive crash
  • Being able to try something out by using a feature branch

This post is an extension of the Secure SSH access with certificate based authentication and firewall rules article. As we will use certificate-based authentication to log in to the git server, I would recommend going through this post to set up the Linux server in a similar way.

To indicate where the commands are being executed, all sections are marked with Server or Client.

Installing git

Server

To install git, we have to log in as privileged user (sudoer) and execute

[user@server]$ sudo yum install git-core

Create directory for git repository

Server

To create a base directory for the git repository, we have to log in as the git user.

[git@server]$ mkdir ~/repos

Then we switch to the new directory and initialize an empty git repository

[git@server]$ cd ~/repos
[git@server]$ git init --bare
Initialized empty Git repository in /home/git/repos/

Setup the client

Client

Now we can setup the git client. We first create a local directory which we will use for cloning the git repository.

[git@client]$ mkdir ~/git_repo

Now we can switch to the new directory and can clone the repository.

[git@client]$ ~/git_repo
[git@client]$ git clone git@192.168.1.73:/home/git/repos

Create a new project

Client

We can now create a new project folder and create some initial content, e.g. a README.md file.

[git@client]$ mkdir ~/git_repo/repos/project1
[git@client]$ vi ~/git_repo/repos/project1/README.md

README.md

# Project 1

Project 1 description

Commit the project

Client

Let's perform the first commit to push the changes to our git repository. We usegit add to add files or directories to the staging area. git commit -m "Message to describe the change" is used to check in to our local repository on the client and isgit push used to push the changes to the server.

[git@client]$ git add project1/
[git@client]$ git commit -m "Initial Checkin"
[master (root-commit) 71823d9] Initial Checkin
 1 file changed, 3 insertions(+)
 create mode 100644 project1/README.md
[git@client]$ git push
warning: push.default is unset; its implicit value has changed in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the traditional behavior, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.

Since Git 2.0, Git defaults to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Counting objects: 4, done.
Writing objects: 100% (4/4), 286 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To git@192.168.1.73:/home/git/repos
 * [new branch]      master -> master

That's it, we have now a fully working git server, have created a repository and checked in our first project.

Web Frontend for the git server

In case you would like to use a web frontend to access your git repositories, Gogs is an open source and free of charge solution. It is easy to setup, but I will not cover the setup process in this post.