Hosting + Ai Website Builder + Free Domain (3 Month Free Credit)
Shop Today

How to Use Git on Linux for Version Control [Complete Guide]

Use Git on a Linux server to manage source code versions, collaborate in software development, and keep track of changes efficiently. Git is a distributed version control system that enables multiple developers to work on projects simultaneously without conflicts. Setting up Git on a Linux server allows you to host Git repositories locally, enabling private project collaboration and secure version control without relying on third-party services.

Use Git on Linux

This guide explains how to install and use Git, create and manage repositories, and configure basic access on a Linux server.

Prerequisites

Before getting started, make sure you have the following:

  • A Linux server (Ubuntu, Debian, CentOS, etc.)
  • A user account with sudo privileges
  • Access to the terminal or SSH
  • Basic understanding of Linux commands
  • Internet access for installing packages

Use Git on a Linux Server

Using Git on a Linux server gives you full control over your repositories and development workflow. You can collaborate with your team, maintain private codebases, and automate project versioning all without relying on platforms like GitHub or GitLab. Whether for personal projects or enterprise development, a local Git server is fast, secure, and customizable.

Install Git on the Linux Server

Install Git using your Linux distribution’s package manager.

sudo apt update
sudo apt install git
  • CentOS/RHEL/Fedora:
sudo yum install git

Or on newer systems:

sudo dnf install git

Verify the installation:

git --version

Create a Dedicated Git User

For security and organization, create a new user to manage Git repositories:

sudo adduser git

Set a password and provide the requested information, or press Enter to skip.

Set Up Repository Storage Directory

Create a directory where Git repositories will be stored. For example:

sudo mkdir -p /usr/local/git
sudo chown git:git /usr/local/git

Switch to the git user:

sudo su - git

Also, Check | Understand Git on Linux Server: The Essential Guide

Initialize a Bare Git Repository

bare repository is a repository without a working directory, suitable for a central repository that users push to and pull from.

Create a new bare repository for your project:

cd /usr/local/git
git init --bare myproject.git

This creates a new repository named myproject.git in /usr/local/git directory.

Configure SSH Access for Git User

To allow secure Git access using SSH:

  • On your local machine, generate SSH keys if you don’t have them already:
ssh-keygen -t rsa -b 4096
  • Copy your public key to the Linux server (replace gitserver and git user as applicable):
ssh-copy-id git@your_server_ip

Alternatively, manually append your local public key (~/.ssh/id_rsa.pub) to /home/git/.ssh/authorized_keys on the server.

This enables password-less SSH login for the git user.

Clone the Repository from Your Local Machine

On your local development machine, clone the repository to start working:

git clone git@your_server_ip:/usr/local/git/myproject.git

Replace your_server_ip with or hostname accordingly.

Use Git Commands Normally

After cloning, you can use standard Git commands:

  • Add files and commit changes:
git add file1.txt
git commit -m "Initial commit"
  • Push changes back to the server:
git push origin main
  • Pull changes:
git pull origin main

Manage Repository Permissions and Security

  • Ensure the git user is the owner of repositories:
sudo chown -R git:git /usr/local/git/myproject.git
  • Configure the SSH AllowUsers directive to restrict SSH access to only the git user (optional):

Edit /etc/ssh/sshd_config:

AllowUsers git

Restart SSH service:

sudo systemctl restart sshd

Conclusion

To use Git on a Linux server, install Git software using your distro’s package manager, create a dedicated git user and repository directory, initialize bare repositories for remote hosting, and configure SSH access for secure user authentication. Cloning repositories over SSH allows you to perform distributed version control efficiently and collaboratively. This setup provides a private, flexible Git server without relying on third-party platforms. For more, visit git official documentation.

Himanshu Joshi

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top