Last modified: July 10, 2024

This article is written in: 🇺🇸

Setting Up Your Own Git Server

Creating your own Git server offers increased control, enhanced security, and a tailor-made environment for your repositories. It's a great alternative to relying on services like GitHub or GitLab, especially for personal projects or within an organization. Here's an expanded guide to set up a Git server on a Debian-based system.

+-------------------+              +-------------------+
|   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     |
         |                                |
         +--------------------------------+

Pre-requisites

When preparing to set up the server, certain requirements and configurations are essential:

Installing Git

To get Git installed on your server:

sudo apt update
sudo apt install git-core

Setting Up a Repository

I. Decide on a location for your repositories

Choose a directory where your repositories will be stored. For this guide, we are using /opt/git/:

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

This command creates the directory structure for your repository.

II. Move to the Directory

Navigate to the newly created directory:

cd /opt/git/myrepo.git

This changes your current directory to the location where you will set up your repository.

III. Initialize as a Bare Repository

Set up the repository as a bare repository, which is suitable for sharing:

sudo git init --bare

A bare repository does not have a working directory and is used for remote repository access.

Configuring User Access

For enhanced security, it is recommended to use a dedicated git user.

I. Create the Git User

Create a new user named git:

sudo adduser git

This command creates a new user account named git.

II. Assign a Password

Set a password for the git user:

sudo passwd git

Follow the prompts to set and confirm the password.

III. Prepare SSH for the Git User

To enable SSH access for the git user, perform the following steps:

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

IV. Add Authorized Users

Add the public SSH keys of users who are allowed to access the repository:

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

Replace public_key_content with the actual public SSH key from the user's client machine. This allows the specified users to authenticate via SSH.

Using the Repository

With the server ready, users can now clone, push, and pull:

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

Replace yourserver with your server's IP or domain name.

Additional Tips for a Robust Git Server

Table of Contents

  1. Setting Up Your Own Git Server
    1. Pre-requisites
    2. Installing Git
    3. Setting Up a Repository
      1. I. Decide on a location for your repositories
      2. II. Move to the Directory
      3. III. Initialize as a Bare Repository
    4. Configuring User Access
      1. I. Create the Git User
      2. II. Assign a Password
      3. III. Prepare SSH for the Git User
      4. IV. Add Authorized Users
  2. Using the Repository
    1. Additional Tips for a Robust Git Server