How to Set Up a Git Server on Your Dedicated Server

Setting up a Git server on your dedicated server allows you to manage and store your code repositories in a secure and private environment. This guide will walk you through the steps to install and configure a Git server on your dedicated server.

Step 1: Install Git on Your Dedicated Server

  • Connect to your dedicated server using SSH.

  • Update your package list to ensure you have the latest software versions:

    sudo apt update && sudo apt upgrade -y  # For Debian/Ubuntu
    sudo yum update -y                     # For CentOS/RHEL
    
  • Install Git using the following command:

    sudo apt install git -y   # For Debian/Ubuntu
    sudo yum install git -y   # For CentOS/RHEL
    
  • Verify the installation:

    git --version
    

Step 2: Create a Git User for Repository Management

  • To keep repositories organized and secure, create a dedicated Git user:

    sudo adduser git
    
  • Set a strong password for the Git user and complete the account setup.

Step 3: Set Up SSH Access for Git

  • Switch to the Git user account:

    sudo su - git
    
  • Create an SSH directory:

    mkdir -p ~/.ssh && chmod 700 ~/.ssh
    
  • Add authorized keys for users who should have access:

    nano ~/.ssh/authorized_keys
    
  • Paste the public SSH keys of users who will connect to the Git server and save the file.

  • Set the correct permissions:

    chmod 600 ~/.ssh/authorized_keys
    

Step 4: Create a Bare Git Repository

  • Navigate to the home directory of the Git user:

    cd /home/git
    
  • Create a new repository:

    mkdir myrepo.git
    cd myrepo.git
    git init --bare
    
  • This initializes a bare repository, which is designed for remote collaboration.

Step 5: Set Up Permissions for Repository Access

  • Ensure that only the Git user can modify repository files:

    chown -R git:git /home/git/myrepo.git
    

Step 6: Clone the Git Repository on a Local Machine

  • On your local computer, run the following command to clone the repository:

    git clone git@gitserver:/home/git/myrepo.git
    
    • Replace gitserver with your server’s IP address or hostname.
    • You may need to confirm the SSH key fingerprint on the first connection.

Step 7: Push Code to the Git Server

  • After cloning, navigate into the repository:

    cd myrepo
    
  • Add a new file and commit it:

    echo "Welcome to my Git server" > README.md
    git add README.md
    git commit -m "Initial commit"
    
  • Push the changes to the server:

    git push origin master
    

Step 8: Manage User Access and Repositories

  • To create additional repositories, repeat Step 4.
  • To add more users, append their SSH keys to ~/.ssh/authorized_keys.
  • Consider using Gitolite or Gitea for advanced repository management and access control.

By setting up a Git server on your dedicated server, you gain complete control over your repositories, ensuring privacy, security, and efficiency in your development workflow.

Was this answer helpful? 0 Users Found This Useful (0 Votes)