Last modified: January 14, 2025

This article is written in: 🇺🇸

Git Server

Setting up your own Git server allows you to manage your version control system in-house, giving you control over where repositories are stored and how access is managed. By hosting your own server, you can customize the environment to better fit your team’s workflow, implement specific security measures, and scale resources according to your needs. This approach provides a flexible foundation for collaboration, enabling you to tailor the setup to match your project requirements.

Setting up your own Git server provides several benefits compared to relying on hosted platforms:

Below is a high-level diagram of how various Git clients interact with a central bare repository:

+-------------------+              +-------------------+
|   Git Client 1    |              |   Git Client 2    |
| - Clone           |              | - Push            |
| - Pull            |              | - Pull Requests   |
| - Push            |              | - Merge           |
+--------+----------+              +----------+--------+
         |                                   |
         |                                   |
         +----+----------------------+-------+
              |                      |
              |    Git Operations    |
              |                      |
         +----v----------------------v----+
         |                                |
         |       Git Server (Bare)        |
         | - Central Repository Storage   |
         | - Access Control               |
         | - Version History Management   |
         | - Branches & Tags Handling     |
         |                                |
         +--------------------------------+

Prerequisites

Install Git

I. Update Package Lists

sudo apt update

II. Install Git

sudo apt install git-core

This installs the latest version of Git available in your distribution’s package repositories.

Set Up a Bare Repository

A bare repository is a central repository that does not contain a working directory (i.e., no files you can directly edit). Instead, it stores the Git version control data, making it ideal for sharing among multiple developers or for deployment processes.

Create a Directory for Your Repositories

Decide on a location for your repositories. Below, we’re using /opt/git/ for organizational purposes:

sudo mkdir -p /opt/git/myrepo.git

cd /opt/git/myrepo.git

Initialize a Bare Repository

sudo git init --bare

Your new bare repository is now located at /opt/git/myrepo.git. Other team members (or even you on another machine) can clone and push to this remote repository.

Configure User Access

Create a Dedicated Git User

For enhanced security and simplified access management, set up a dedicated git user:

sudo adduser git

Follow the prompts to configure the new user’s details.

Set a Password for the Git User

sudo passwd git

You can optionally skip setting a password if you plan to rely solely on SSH keys.

Configure SSH for the Git User

To allow key-based SSH authentication, switch to the git user and set up their .ssh directory:

sudo su git
mkdir ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

Authorize Additional Users

To grant another developer access to this Git server, append their public SSH key to the authorized_keys file:

echo "public_key_content" >> ~/.ssh/authorized_keys

Replace public_key_content with the actual public key string (e.g., the contents of the user’s id_rsa.pub file). From now on, these users can authenticate as git via SSH.

Using the Repository

With the server ready, developers can clone and work with the repository:

git clone git@yourserver:/opt/git/myrepo.git

Once cloned, they can pull and push changes:

# Pull latest changes
git pull

# Stage changes, commit, and push
git add .
git commit -m "Update project files"
git push

Additional Tips for a Robust Git Server

Table of Contents

    Git Server
    1. Prerequisites
    2. Install Git
    3. Set Up a Bare Repository
      1. Create a Directory for Your Repositories
      2. Navigate into the Directory
      3. Initialize a Bare Repository
    4. Configure User Access
      1. Create a Dedicated Git User
      2. Set a Password for the Git User
      3. Configure SSH for the Git User
      4. Authorize Additional Users
    5. Using the Repository
    6. Additional Tips for a Robust Git Server