Back to Git & Dev Tools
2025-12-039 min read

Git on the Server (Git & Dev Tools)

Learn Git on the Server (Git & Dev Tools) step by step with clear examples and exercises.

Title: Git on the Server (Git & Dev Tools)

Why This Matters

In software development, collaboration and version control are crucial for managing projects with multiple contributors. Git, a popular distributed version control system, has become an industry standard for this purpose. However, to truly harness its power, you need to understand how to use it on a server—allowing your entire team to work seamlessly together. This lesson will delve into the practical aspects of setting up Git on a server, working with remote repositories, and common pitfalls to avoid.

Prerequisites

Before diving into Git on the Server, it is essential that you have a solid understanding of the following:

  1. Basic Git commands (e.g., git init, git add, git commit, git pull, and git push)
  2. SSH keys for secure communication between clients and servers
  3. Familiarity with your server's operating system (Linux-based systems are commonly used)
  4. Basic understanding of networking concepts, such as ports and firewall rules, to allow Git traffic on the server
  5. Understanding of how to configure a web server like Apache or Nginx to serve static files from your project
  6. Knowledge of how to set up SSL/TLS certificates for secure communication between clients and servers
  7. Comprehension of Git workflows, such as feature branches and pull requests
  8. Familiarity with using a terminal or command-line interface on both the server and local machine

Core Concept

Git on the Server involves setting up a central repository that acts as a single source of truth for your project, allowing multiple developers to collaborate effectively. There are several ways to achieve this, but we will focus on two primary methods: Git Daemon and Smart HTTP.

Git Daemon

Git Daemon is a standalone server that allows read-only access to your Git repositories over the network. It's lightweight and easy to set up, making it an ideal choice for small teams or personal projects. To install Git Daemon on your server:

  1. Install Git if it isn't already installed: sudo apt-get install git (for Ubuntu)
  2. Start the Git daemon service: sudo git daemon --verbose --base-path=/path/to/your/repo --reuseaddr=6397
  3. Configure Git to automatically start at boot: sudo systemctl enable git-daemon
  4. Access the repository from your local machine using the command: git clone user@server:/path/to/your/repo.git

Configuring Git Daemon for a Specific Port

If port 6397 is already in use on your server, you can specify a different port by modifying the command as follows:

sudo git daemon --verbose --base-path=/path/to/your/repo --reuseaddr=<custom_port>

Replace `` with your desired port number.

Smart HTTP

Smart HTTP is another method for serving Git repositories over the network, but it allows both read and write access. This makes it more suitable for larger teams or projects with frequent updates. To set up a Smart HTTP server:

  1. Install Git if it isn't already installed: sudo apt-get install git (for Ubuntu)
  2. Navigate to your repository directory: cd /path/to/your/repo
  3. Create a new configuration file (if one doesn't exist): touch .git/config
  4. Edit the config file and add the following lines:
[http]
post-receive = git-receive-pack --keep-unchanged-track --advertise-refs
[receive]
denyCurrentBranch = ignore
  1. Save the file and exit the editor
  2. Start the Smart HTTP server: git init --bare (this will create a new bare repository)
  3. Access the repository from your local machine using the command: git clone user@server:/path/to/your/repo.git

Configuring Git for Secure Communication

To ensure secure communication between clients and servers, it is recommended to use SSH keys instead of HTTPS. To do this, follow these steps:

  1. Generate an SSH key pair on your local machine: ssh-keygen
  2. Copy the public key (usually located at ~/.ssh/id_rsa.pub) to your server: ssh-copy-id user@server
  3. Modify the Git configuration file on the server to allow SSH access:
[core]
sshCommand = "ssh -i /path/to/your/ssh_key"

Replace /path/to/your/ssh_key with the path to your private SSH key.

Worked Example

Let's walk through setting up a Git repository on a server and cloning it to our local machine using both Git Daemon and Smart HTTP methods.

Git Daemon Example

  1. Install Git on the server: sudo apt-get install git
  2. Navigate to your project directory: cd /path/to/your/project
  3. Initialize a new Git repository: git init
  4. Start the Git daemon service with a custom port (e.g., 9001): sudo git daemon --verbose --base-path=/path/to/your/project --reuseaddr=9001
  5. On your local machine, clone the repository using SSH: git clone user@server:/path/to/your/project.git

Smart HTTP Example

  1. Install Git on the server: sudo apt-get install git
  2. Navigate to your project directory: cd /path/to/your/project
  3. Initialize a new bare repository: git init --bare
  4. Generate an SSH key pair on your local machine: ssh-keygen
  5. Copy the public key (usually located at ~/.ssh/id_rsa.pub) to your server and append it to the authorized_keys file in the server's ~/.ssh directory.
  6. Modify the Git configuration file on the server to allow SSH access:
[core]
sshCommand = "ssh -i /path/to/your/ssh_key"

Replace /path/to/your/ssh_key with the path to your private SSH key.

  1. Access the repository from your local machine using the command: git clone user@server:/path/to/your/project.git

Common Mistakes

Forgetting to Start Git Daemon or Smart HTTP Server at Boot

Ensure that the Git daemon or Smart HTTP server starts automatically when your server boots up by using the appropriate systemd command (e.g., sudo systemctl enable git-daemon).

Incorrect Configuration Options

Double-check your configuration options, especially in the .git/config file for Smart HTTP servers, to ensure that they are correctly set up for your needs.

Failing to Use SSH Keys

Always use SSH keys for secure communication between clients and servers when working with Git on the Server.

Common Mistakes - Git Daemon Specific

  1. Not specifying a custom port: If port 6397 is already in use, you may encounter errors or timeouts when trying to access the repository. Make sure to specify a different port if necessary.
  2. Forgetting to configure Git to start at boot: If the Git daemon isn't started automatically at boot, your team members won't be able to clone the repository until someone manually starts it.

Practice Questions

  1. What is the purpose of using Git on a server?
  • To allow multiple developers to work on the same project simultaneously
  • To create a backup of your local repositories on a remote server
  • To improve the performance of your local Git repository
  • To store sensitive data securely on a remote server
  1. What are two methods for serving Git repositories over the network?
  • FTP and SSH
  • Smart HTTP and Git Daemon
  • GitLab and Bitbucket
  • GitHub and Apache
  1. Why is it important to use SSH keys for secure communication between clients and servers when working with Git on the Server?
  • SSH keys are less susceptible to interception and man-in-the-middle attacks compared to HTTPS
  • SSH keys provide a more efficient method of communication than HTTPS
  • SSH keys are required for Git repositories hosted on servers
  • SSH keys allow you to access Git repositories without a password
  1. What steps are required to configure Git Daemon for a custom port?
  • Modify the command used to start the daemon by replacing 6397 with your desired port number
  • Install Git on the server and create a new configuration file in the repository directory
  • Generate an SSH key pair on your local machine and copy the public key to your server's ~/.ssh/authorized_keys file
  • Start the Smart HTTP server on your local machine and access the repository using Git commands
  1. How can you ensure that the Git daemon or Smart HTTP server starts automatically at boot?
  • Use the appropriate systemd command (e.g., sudo systemctl enable git-daemon)
  • Modify the configuration options in the .git/config file to include startup commands
  • Create a new script that starts the Git daemon or Smart HTTP server and add it to your server's cron jobs
  • Set up an automatic update for the Git daemon or Smart HTTP server using a package manager like apt-get

FAQ

Q1: What is the purpose of using Git on a server?

A1: The primary purpose of using Git on a server is to create a central repository that acts as a single source of truth for your project, allowing multiple developers to collaborate effectively.

Q2: How can you set up a read-only Git repository using Git Daemon?

A2: To set up a read-only Git repository using Git Daemon, follow the steps outlined in the "Core Concept" section of this lesson.

Q3: How can you set up a read-write Git repository using Smart HTTP?

A3: To set up a read-write Git repository using Smart HTTP, follow the steps outlined in the "Core Concept" section of this lesson and ensure that you have configured your server to allow write access.

Q4: What are some common mistakes to avoid when setting up Git on a server?

A4: Some common mistakes to avoid include forgetting to start the Git daemon or Smart HTTP server at boot, using incorrect configuration options, and failing to use SSH keys for secure communication between clients and servers.

Q5: Why is it important to use SSH keys for secure communication between clients and servers?

A5: Using SSH keys provides a more secure method of communication than HTTPS because they are less susceptible to interception and man-in-the-middle attacks.

Q6: What steps are required to configure Git Daemon for a custom port?

A6: To configure Git Daemon for a custom port, modify the command used to start the daemon by replacing 6397 with your desired port number.

Q7: How can you ensure that the Git daemon or Smart HTTP server starts automatically at boot?

A7: You can ensure that the Git daemon or Smart HTTP server starts automatically at boot by using the appropriate systemd command (e.g., sudo systemctl enable git-daemon).

Q8: What additional steps are required to configure Git for SSH access with Smart HTTP?

A8: To configure Git for SSH access with Smart HTTP, generate an SSH key pair on your local machine, copy the public key to your server, and modify the Git configuration file on the server to allow SSH access.

Q9: Explain the difference between a bare repository and a non-bare repository in the context of Git on the Server.

A9: A bare repository is a Git repository that does not contain a working directory, while a non-bare repository contains both a working directory and a repository. In the context of Git on the Server, you would typically use a bare repository for the central repository that multiple developers access.

Q10: Describe how to handle merge conflicts when working with multiple developers on a shared Git repository.

A10: To handle merge conflicts when working with multiple developers on a shared Git repository, resolve the conflicts manually by editing the affected files and committing the changes. You can also use tools like GitHub's merge conflict resolution interface or Git's built-in merge tool to help you resolve conflicts more easily.

Q11: What is a Git hook, and what purpose does it serve in Git on the Server?

A11: A Git hook is a script that Git executes automatically under certain conditions, such as when a repository is updated or a commit is made. In Git on the Server, you can use hooks to automate tasks like sending notifications when changes are pushed to the repository or enforcing specific policies for commits.

Q12: How can you implement access control for users who should have read-only or read-write permissions on specific branches of your

Git on the Server (Git & Dev Tools) | Git & Dev Tools | XQA Learn