Installing a Git Server

GitGitBeginner
Practice Now

Introduction

Git is a powerful, open-source distributed version control system that is crucial for managing project versions efficiently. Setting up your own Git server not only enhances your understanding of Git's internal mechanisms but also provides a secure environment for private project management. This lab guides you through the process of setting up a Git server on a Linux machine. Despite the availability of platforms like GitHub for hosting code, a private Git server ensures your data remains secure under your control.

๐ŸŽฏ Tasks

In this project, you will learn:

  • How to install and configure Git on a Linux system
  • How to create and manage a user specifically for Git operations
  • How to generate and configure SSH keys to secure connections between your local machine and the Git server
  • How to initialize and manage a Git repository locally and on a server
  • How to perform version control operations such as committing changes and pushing them to a remote repository
  • How to verify the setup by cloning the repository to ensure everything is functioning correctly

๐Ÿ† Achievements

After completing this project, you will be able to:

  • Understand the fundamentals of installing and setting up Git, a distributed version control system, on a Linux environment
  • Handle user permissions and security in a Unix/Linux system for application-specific tasks
  • Utilize techniques for secure communication between machines using SSH keys, an essential skill for managing remote servers
  • Create and manage bare Git repositories which are used for server-side storage in Git operations
  • Commit and version your projects using Git commands, and push these changes to a remote server
  • Verify and troubleshoot common issues in remote repository management by cloning and inspecting the integrity of the data

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) git(("`Git`")) -.-> git/SetupandConfigGroup(["`Setup and Config`"]) linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) git(("`Git`")) -.-> git/BasicOperationsGroup(["`Basic Operations`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) git(("`Git`")) -.-> git/CollaborationandSharingGroup(["`Collaboration and Sharing`"]) linux(("`Linux`")) -.-> linux/RemoteAccessandNetworkingGroup(["`Remote Access and Networking`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/InputandOutputRedirectionGroup -.-> linux/pipeline("`Data Piping`") linux/InputandOutputRedirectionGroup -.-> linux/redirect("`I/O Redirecting`") git/SetupandConfigGroup -.-> git/init("`Initialize Repo`") git/SetupandConfigGroup -.-> git/clone("`Clone Repo`") linux/PackagesandSoftwaresGroup -.-> linux/apt("`Package Handling`") git/BasicOperationsGroup -.-> git/add("`Stage Files`") git/BasicOperationsGroup -.-> git/commit("`Create Commit`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/UserandGroupManagementGroup -.-> linux/useradd("`User Adding`") linux/UserandGroupManagementGroup -.-> linux/passwd("`Password Changing`") linux/UserandGroupManagementGroup -.-> linux/su("`User Switching`") git/SetupandConfigGroup -.-> git/config("`Set Configurations`") linux/BasicFileOperationsGroup -.-> linux/chown("`Ownership Changing`") git/CollaborationandSharingGroup -.-> git/push("`Update Remote`") git/CollaborationandSharingGroup -.-> git/remote("`Manage Remotes`") linux/RemoteAccessandNetworkingGroup -.-> linux/ssh("`Secure Connecting`") subgraph Lab Skills linux/cat -.-> lab-299593{{"`Installing a Git Server`"}} linux/echo -.-> lab-299593{{"`Installing a Git Server`"}} linux/pipeline -.-> lab-299593{{"`Installing a Git Server`"}} linux/redirect -.-> lab-299593{{"`Installing a Git Server`"}} git/init -.-> lab-299593{{"`Installing a Git Server`"}} git/clone -.-> lab-299593{{"`Installing a Git Server`"}} linux/apt -.-> lab-299593{{"`Installing a Git Server`"}} git/add -.-> lab-299593{{"`Installing a Git Server`"}} git/commit -.-> lab-299593{{"`Installing a Git Server`"}} linux/cd -.-> lab-299593{{"`Installing a Git Server`"}} linux/mkdir -.-> lab-299593{{"`Installing a Git Server`"}} linux/useradd -.-> lab-299593{{"`Installing a Git Server`"}} linux/passwd -.-> lab-299593{{"`Installing a Git Server`"}} linux/su -.-> lab-299593{{"`Installing a Git Server`"}} git/config -.-> lab-299593{{"`Installing a Git Server`"}} linux/chown -.-> lab-299593{{"`Installing a Git Server`"}} git/push -.-> lab-299593{{"`Installing a Git Server`"}} git/remote -.-> lab-299593{{"`Installing a Git Server`"}} linux/ssh -.-> lab-299593{{"`Installing a Git Server`"}} end

Installing Git

In this step, we'll install Git on your machine, which will serve as both the local and remote server. This exercise will prepare you to potentially set up a Git server on your personal computer for local network hosting.

## Update the apt package database to ensure we are working with the latest software list
sudo apt-get update

## Install Git using apt-get
sudo apt-get install git

This command updates your package list to make sure all subsequent installations are up to date, and then installs Git, the version control system that will manage our repositories.

Configuring the Git User

To facilitate management, we'll create a new user specifically for Git operations and set up their working directory.

## Create a new user for Git operations
sudo useradd labex-git
sudo passwd labex-git

Set a password for the Git user(like "labex"), you will need to enter the password twice.

Next, create a working directory for the Git user and assign ownership

sudo mkdir /home/labex-git
sudo chown -R labex-git:labex-git /home/labex-git

This series of commands creates a new user, sets a password, and establishes a dedicated working directory for Git operations, ensuring that repository management is isolated from other system activities.

Generating SSH Keys

Using SSH keys will simplify our Git operations by eliminating the need to enter user credentials with every operation. Letโ€™s generate an SSH key pair and set it up.

## Generate an SSH key pair
ssh-keygen -t rsa -C "labex@labex.io"

Press enter to all prompts to use default settings. Then, send the public key to the Git user's machine.

cat ~/.ssh/id_rsa.pub | ssh labex-git@localhost 'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys'

Here you should enter "yes" and press the password you have set before.

This step generates an SSH key pair, securing the connection between your local and remote setups. The public key is then added to the authorized list on the Git server, enabling secure communication without needing a password for each operation.

Creating a Remote Repository

Now, we'll switch to the Git user and set up a bare Git repository which is suitable for server-side storage.

## Switch to the Git user
su labex-git

Enter the password you have set.

Now, you can create a repository folder and initialize a bare repository.

mkdir -p /home/labex-git/project.git
cd /home/labex-git/project.git
git init --bare

This command sequence switches the current user to the Git user, creates a new directory for your repository, and initializes a bare repository. This setup is crucial for the Git server as it will store all data sent to it without maintaining any working copies, making it ideal for pushing and pulling data.

Setting Up the Local Repository

In this step, we'll set up the local repository on your machine. This process mirrors what you would typically do on a code hosting platform.

First, switch back to your regular user from the Git user:

## Exit from Git user
exit

Now, let's create a directory for your project and initialize a Git repository:

## Create a directory for your project
mkdir -p /home/labex/project/labex_project
cd /home/labex/project/labex_project

## Initialize a Git repository
git init

This sets up a new Git repository in the specified directory, which will serve as your local repository for the project.

Configure your Git user details:

## Set your Git username
git config --global user.name "labex"

## Set your Git email
git config --global user.email "labex@labex.io"

This step ensures that all your commits will be recorded with your username and email, making it easier to track changes.

Create a file to test the commit process:

## Create a test document
echo "I am Labex Readme Doc" > readme.md

This command creates a simple text file named readme.md containing a sample message, which we will use to test the commit and push process to the remote repository.

Committing and Pushing Changes

Having initialized the repository and set up your Git configuration, it's time to commit and push your first changes.

## Add all changes to staging
git add .

## Commit the changes
git commit -m "Initial commit"

## Add remote repository
git remote add origin labex-git@localhost:/home/labex-git/project.git

Now, we can push the changes to the remote repository.

git push origin master

Verifying the Setup

Finally, letโ€™s clone the repository to ensure that your commits are properly hosted on the remote server.

## Navigate to the cloning directory
cd /home/labex/project

## Clone the repository
git clone labex-git@localhost:/home/labex-git/project.git

You can see that a new repository has been cloned in the /home/labex/project path, and the readme.md file in it.

Summary

In this lab, you've learned how to set up and configure a Git server on a Linux system, manage SSH keys, and perform typical Git operations like initializing repositories, committing changes, and pushing them to a remote server. This exercise demonstrated the complete lifecycle of setting up and using a Git server, providing you with a solid foundation in managing your own code repositories securely. The skills acquired here are not only fundamental for software development but also enhance your understanding of project version control in a private, secure environment.

Other Git Tutorials you may like