Setting Up a Git Server on Your Linux Server

How to install your own Git server on your Linux server.
January 6, 2025 by
Setting Up a Git Server on Your Linux Server
Hamed Mohammadi
| No comments yet

Git is one of the most widely used version control systems today, and hosting your own Git server offers multiple benefits, such as full control over your repositories, privacy, and avoiding dependency on third-party hosting services. Whether you're a developer managing personal projects, or a team handling collaborative development, setting up your own Git server on Linux can be a great way to maintain and secure your codebase.

In this post, we'll walk through the steps required to set up a Git server on a Linux machine. We’ll also explore some free, open-source GitHub or GitLab-like platforms that you can self-host to manage your Git repositories and collaborate with your team.

Prerequisites for Setting Up a Git Server

Before diving into the steps, ensure you have a few basic prerequisites:

  1. A Linux server (e.g., Ubuntu, CentOS, Debian, or any other distribution).
  2. Root or sudo access to the server.
  3. A user account for managing the Git repositories (you can use the root account, but it’s generally safer to create a dedicated user).
  4. SSH access for secure remote connections to the server.

Once you’ve met these requirements, you're ready to get started.

Step-by-Step Guide to Setting Up a Git Server

1. Install Git

First, make sure that Git is installed on your server. Most Linux distributions offer Git through their package manager. Open a terminal and run the following command to install Git:

For Ubuntu/Debian-based systems:

sudo apt update
sudo apt install git

For CentOS/RedHat-based systems:

sudo yum install git

Once the installation is complete, verify it by running:

git --version

2. Create a Git User

For security reasons, it’s best not to run your Git server under the root account. Instead, create a new user specifically for hosting Git repositories.

Run the following command to create a git user:

sudo adduser git

3. Set Up SSH Access

To allow secure connections to the Git server, you’ll need SSH access. You can use public-key authentication for this, which is more secure than password-based authentication.

  1. Generate an SSH Key Pair (on the client machine):

    If you don’t already have an SSH key pair, generate one using the following command on your local machine:

    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    

    This will create a public/private key pair in your ~/.ssh directory.

  2. Copy the Public Key to the Git Server:

    To enable SSH access to your server, copy the public key to the git user’s ~/.ssh/authorized_keys file:

    ssh-copy-id git@your_server_ip
    

    Alternatively, you can manually copy the public key into the git user’s .ssh/authorized_keys file.

4. Create a Git Repository

Once the Git server is set up and SSH access is configured, you can create a Git repository.

  1. Login as the Git user:

    sudo su - git
    
  2. Create a directory for your repository:

    mkdir my_project.git
    cd my_project.git
    
  3. Initialize the repository:

    git init --bare
    

    The --bare flag ensures that this is a Git repository without a working directory, making it suitable for a central repository.

5. Clone the Repository (from your local machine)

Now that the repository is set up on the server, you can clone it to your local machine:

git clone git@your_server_ip:/home/git/my_project.git

6. Push and Pull Changes

Once the repository is cloned, you can start pushing and pulling changes just like any other Git project. For example, to push your local changes to the server:

git push origin master

Free and Open-Source GitHub/GitLab-like Platforms

While setting up a basic Git server is sufficient for simple use cases, if you need more advanced features like issue tracking, pull requests, continuous integration, and user management, you can opt for a more comprehensive platform like GitHub or GitLab, but self-hosted. There are several free, open-source alternatives you can set up on your own server to enjoy these features.

GitLab

GitLab is one of the most popular open-source alternatives to GitHub. It provides a robust platform for Git repository management, issue tracking, continuous integration, and deployment pipelines. With GitLab, you can host multiple repositories and collaborate with your team on software projects.

To install GitLab on your Linux server, you can follow the instructions provided on the official GitLab website. In summary, you’ll need to run the following commands:

For Ubuntu:

sudo apt-get install -y curl openssh-server ca-certificates
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.deb.sh | sudo bash
sudo EXTERNAL_URL="http://your_domain_or_ip" apt-get install gitlab-ee

GitLab’s built-in features like issue tracking, merge requests, and CI/CD pipelines make it a powerful tool for teams of all sizes. You can also customize GitLab with various plugins and integrations to enhance functionality.

Gitea

Gitea is a lightweight and self-hosted Git service that provides many of the same features as GitHub and GitLab but is known for being fast and simple to deploy. It is a perfect solution for small teams and individuals who want a self-hosted Git server with web interface capabilities.

Installing Gitea on a Linux server is straightforward, and you can follow these steps:

  1. Download the latest Gitea release:

    wget -O /usr/local/bin/gitea https://dl.gitea.io/gitea/latest/gitea-linux-amd64
    chmod +x /usr/local/bin/gitea
    
  2. Set up a systemd service to run Gitea as a service:

    sudo systemctl enable --now gitea
    

Gitea provides a simple yet effective web interface, support for multiple Git repositories, issue tracking, pull requests, and many integrations, making it a solid choice for teams looking for an open-source Git management solution.

Phabricator

Phabricator is another open-source platform that offers Git hosting along with various collaboration tools, including bug tracking, code review, and project management. It’s ideal for development teams needing a robust suite of tools beyond just Git repository management.

To install Phabricator on a Linux server, you can follow the official installation instructions, but typically it involves installing dependencies such as Apache, MySQL, and PHP, and then configuring the application on your server.

Conclusion

Setting up your own Git server on a Linux machine offers numerous advantages, including greater control, security, and cost savings. While a basic Git setup may be sufficient for personal projects, using a full-fledged, self-hosted platform like GitLab, Gitea, or Phabricator can enhance your development workflow by adding powerful collaboration tools, issue tracking, and CI/CD pipelines. Whether you're managing a small project or a large development team, hosting Git repositories on your own server is a great way to keep your code safe and ensure smooth collaboration.

Setting Up a Git Server on Your Linux Server
Hamed Mohammadi January 6, 2025
Share this post
Tags
Archive

Please visit our blog at:

https://zehabsd.com/blog

A platform for Flash Stories:

https://readflashy.com

A platform for Persian Literature Lovers:

https://sarayesokhan.com

Sign in to leave a comment