Create a Bare repository:
git init --bare myproject.git
Step 2: Cloning the Repository to a Local Machine Clone the repository to your local machine using the `git clone` command.
git clone gituser@your_vps_ip_address:/var/git/myproject.git
Replace «gituser» with the username you created, and «your_vps_ip_address» with the IP address of your VPS. Step 3: Sending Changes to the Server After making changes to the repository on your local machine, you can send them to the server using the `git push` command.
git push origin main
This method is easy to set up but does not provide advanced access control. Every user who has access to the `gituser` account can modify all repositories.

Setting up Gitolite

Gitolite is a more advanced way to set up a Git server that provides a flexible access control system. Step 1: Installing Gitolite Log in to the VPS as the `gituser` user and clone the Gitolite repository.
git clone https://github.com/sitaramc/gitolite.git
mkdir -p ~/bin
mv gitolite ~/bin/
Then run the Gitolite installer.
~/bin/gitolite install
Step 2: Configuring Gitolite Run Gitolite using your public SSH key.
gitolite setup -pk ~/.ssh/id_rsa.pub
Replace `~/.ssh/id_rsa.pub` with the path to your public SSH key. Step 3: Managing Repositories Now you can manage repositories and access rights using the `gitolite-admin` repository. Clone it to your local computer.
git clone gituser@your_vps_ip_address:gitolite-admin
The `gitolite-admin` repository contains two directories: `conf` and `keydir`. The `conf` directory contains repository configuration files, and the `keydir` directory contains users’ public SSH keys. To create a new repository, add a configuration file to the `conf` directory and the user’s SSH key to the `keydir` directory. Then send the changes to the `gitolite-admin` repository. Example configuration file `conf/gitolite.conf`:
repo myproject
    RW+ = @all
This configuration grants read and write access to all users. You can configure more complex access rules using user groups and different access rights.

Example: Let’s develop a simple Python script, add it to a Git repository, make a commit, and send it to the remote repository. First, create a `hello.py` file:
nano hello.py
Add the following code:
print("Привет, мир!")
Add it to Git then:
git add hello.py
git commit -m "Added hello.py"
git push origin main
For the first `push` you may need to specify `—set-upstream`:
git push --set-upstream origin main

Setting up a Git Server (optional)

Instead of using public services such as GitHub or GitLab, you can set up your own Git server on Your VPS Operating System" class="internal-post-link">Your VPS IP Address" class="internal-post-link">your VPS. This gives you complete control over your repositories and allows you to store confidential code on your own infrastructure. We will look at setting up a Git server using SSH, Gitolite, and Gitea.

Setting up a Git Server using SSH

The easiest way to set up a Git server is to use SSH. This method is suitable for small teams and projects where a complex access control system is not required. Step 1: Creating a Repository Create a directory to store Git repositories. For example:
sudo mkdir /var/git
sudo chown gituser:gituser /var/git
chmod 777 /var/git
cd /var/git
Create a Bare repository:
git init --bare myproject.git
Step 2: Cloning the Repository to a Local Machine Clone the repository to your local machine using the `git clone` command.
git clone gituser@your_vps_ip_address:/var/git/myproject.git
Replace «gituser» with the username you created, and «your_vps_ip_address» with the IP address of your VPS. Step 3: Sending Changes to the Server After making changes to the repository on your local machine, you can send them to the server using the `git push` command.
git push origin main
This method is easy to set up but does not provide advanced access control. Every user who has access to the `gituser` account can modify all repositories.

Setting up Gitolite

Gitolite is a more advanced way to set up a Git server that provides a flexible access control system. Step 1: Installing Gitolite Log in to the VPS as the `gituser` user and clone the Gitolite repository.
git clone https://github.com/sitaramc/gitolite.git
mkdir -p ~/bin
mv gitolite ~/bin/
Then run the Gitolite installer.
~/bin/gitolite install
Step 2: Configuring Gitolite Run Gitolite using your public SSH key.
gitolite setup -pk ~/.ssh/id_rsa.pub
Replace `~/.ssh/id_rsa.pub` with the path to your public SSH key. Step 3: Managing Repositories Now you can manage repositories and access rights using the `gitolite-admin` repository. Clone it to your local computer.
git clone gituser@your_vps_ip_address:gitolite-admin
The `gitolite-admin` repository contains two directories: `conf` and `keydir`. The `conf` directory contains repository configuration files, and the `keydir` directory contains users’ public SSH keys. To create a new repository, add a configuration file to the `conf` directory and the user’s SSH key to the `keydir` directory. Then send the changes to the `gitolite-admin` repository. Example configuration file `conf/gitolite.conf`:
repo myproject
    RW+ = @all
This configuration grants read and write access to all users. You can configure more complex access rules using user groups and different access rights.

Working with a Remote Repository

«` A remote repository is a repository located on a remote server, such as GitHub, GitLab, or Bitbucket. It allows you to collaborate with other developers and store backups of your code. To connect to a remote repository, use the `git remote add` command.
git remote add origin git@github.com:your_username/your_repository.git # Connect to a remote repository
Replace «git@github.com:your_username/your_repository.git» with the URL of your remote repository. «origin» is an alias for the remote repository. You can use any other name. To send local changes to a remote repository, use the `git push` command.
git push origin main # Send the "main" branch to the remote repository "origin"
To retrieve changes from a remote repository, use the `git pull` command.
git pull origin main # Get changes from the "main" branch of the remote repository "origin"
The `git clone` command allows you to clone an existing remote repository to your local machine.
git clone git@github.com:your_username/your_repository.git # Clone the repository
Example: Let’s develop a simple Python script, add it to a Git repository, make a commit, and send it to the remote repository. First, create a `hello.py` file:
nano hello.py
Add the following code:
print("Привет, мир!")
Add it to Git then:
git add hello.py
git commit -m "Added hello.py"
git push origin main
For the first `push` you may need to specify `—set-upstream`:
git push --set-upstream origin main

Setting up a Git Server (optional)

Instead of using public services such as GitHub or GitLab, you can set up your own Git server on your VPS. This gives you complete control over your repositories and allows you to store confidential code on your own infrastructure. We will look at setting up a Git server using SSH, Gitolite, and Gitea.

Setting up a Git Server using SSH

The easiest way to set up a Git server is to use SSH. This method is suitable for small teams and projects where a complex access control system is not required. Step 1: Creating a Repository Create a directory to store Git repositories. For example:
sudo mkdir /var/git
sudo chown gituser:gituser /var/git
chmod 777 /var/git
cd /var/git
Create a Bare repository:
git init --bare myproject.git
Step 2: Cloning the Repository to a Local Machine Clone the repository to your local machine using the `git clone` command.
git clone gituser@your_vps_ip_address:/var/git/myproject.git
Replace «gituser» with the username you created, and «your_vps_ip_address» with the IP address of your VPS. Step 3: Sending Changes to the Server After making changes to the repository on your local machine, you can send them to the server using the `git push` command.
git push origin main
This method is easy to set up but does not provide advanced access control. Every user who has access to the `gituser` account can modify all repositories.

Setting up Gitolite

Gitolite is a more advanced way to set up a Git server that provides a flexible access control system. Step 1: Installing Gitolite Log in to the VPS as the `gituser` user and clone the Gitolite repository.
git clone https://github.com/sitaramc/gitolite.git
mkdir -p ~/bin
mv gitolite ~/bin/
Then run the Gitolite installer.
~/bin/gitolite install
Step 2: Configuring Gitolite Run Gitolite using your public SSH key.
gitolite setup -pk ~/.ssh/id_rsa.pub
Replace `~/.ssh/id_rsa.pub` with the path to your public SSH key. Step 3: Managing Repositories Now you can manage repositories and access rights using the `gitolite-admin` repository. Clone it to your local computer.
git clone gituser@your_vps_ip_address:gitolite-admin
The `gitolite-admin` repository contains two directories: `conf` and `keydir`. The `conf` directory contains repository configuration files, and the `keydir` directory contains users’ public SSH keys. To create a new repository, add a configuration file to the `conf` directory and the user’s SSH key to the `keydir` directory. Then send the changes to the `gitolite-admin` repository. Example configuration file `conf/gitolite.conf`:
repo myproject
    RW+ = @all
This configuration grants read and write access to all users. You can configure more complex access rules using user groups and different access rights.

«`html

How to Install Git on a VPS? A Step-by-Step Guide

Git is an indispensable tool for developers, enabling efficient code version management, collaboration with colleagues, and application deployment. In this article, we will take a detailed look at how to install and configure Git on your Linux-powered VPS (Virtual Private Server). We will cover various installation methods, configure basic parameters, and ensure the security of your repository. Whether you’re creating a new project or working with an existing one, this guide will provide you with all the necessary steps to successfully integrate Git into your VPS infrastructure.

Table of Contents:

VPS Hosting

Virtual servers with guaranteed resources

Choose VPS

Preliminary VPS Preparation

How to install Git on VPS? - Preparing the VPS for Git installation: updating packages, creating a new user, setting up SSH keys.

Before proceeding with Git installation, you need to ensure that your VPS is ready to go. This includes updating the package list, installing the necessary dependencies, and creating a user for working with Git. It is also recommended to configure SSH keys for secure access to the server.

Updating the Package List and Installing Dependencies

The first step is to update the operating system package list. This will allow you to access the latest software versions.
# For Debian/Ubuntu:
sudo apt update && sudo apt upgrade -y

# For CentOS/RHEL:
sudo yum update -y
After updating the package list, you need to install the necessary dependencies that may be required for Git to work. In most cases, these are standard development tools.
# For Debian/Ubuntu:
sudo apt install -y build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev curl git

# For CentOS/RHEL:
sudo yum install -y gcc make zlib-devel openssl-devel bzip2-devel readline-devel sqlite-devel curl git
Make sure the command executes without errors. It is important to install all the specified packages, as the stability of Git may depend on them.

Creating a New User for Git

It is recommended to create a separate user for working with Git to restrict access rights and improve security. This is especially important if your VPS hosts other applications.
sudo adduser gituser
sudo passwd gituser # Set a password for the new user
sudo usermod -aG sudo gituser # Add the user to the sudo group (if necessary)
After creating the user, switch to it for further configuration.
sudo su - gituser
Important: Use a strong password for the new user.

Configuring SSH Keys for Secure Access

Configuring SSH keys will allow you to connect to the VPS without having to enter a password, which increases security and ease of use. First, generate an SSH key on your local machine (if you don’t already have one).
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Replace «your_email@example.com» with your email address. Follow the instructions on the screen to choose where to store the key and enter a passphrase (optional). Then copy the contents of the public key (usually located in the ~/.ssh/id_rsa.pub file) to your VPS. There are several ways to do this. You can use the `ssh-copy-id` command (if it is installed on your local machine):
ssh-copy-id gituser@your_vps_ip_address
Replace «gituser» with the name of the user you created, and «your_vps_ip_address» with the IP address of your VPS. You will be prompted to enter the user’s password on the VPS. If `ssh-copy-id` is not available, you can copy the key manually. First, log in to the VPS as the `gituser` user. Then create a `.ssh` directory (if it doesn’t already exist) and an `authorized_keys` file:
mkdir -p ~/.ssh
nano ~/.ssh/authorized_keys
Paste the contents of your public key into the `authorized_keys` file and save it. Set the correct access rights to the `.ssh` directory and the `authorized_keys` file:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
Now you should be able to connect to the VPS as the `gituser` user without entering a password. Check this by running the following command on your local machine:
ssh gituser@your_vps_ip_address
If everything is configured correctly, you should be logged in without being prompted for a password.

Security comes first! Always use SSH keys to access your VPS and do not store passwords in scripts.

Ivan Petrov, System Administrator
ActionCommand
Update packages (Debian/Ubuntu)sudo apt update && sudo apt upgrade -y
Install Git and dependencies (Debian/Ubuntu)sudo apt install -y build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev curl git
Create a usersudo adduser gituser
Generate an SSH key (locally)ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Installing Git on a VPS (Different Distributions)

How to install Git on VPS? - Comparison of Git installation methods on various Linux distributions (apt, yum, source).

There are several ways to install Git on a Linux-powered VPS. The choice of method depends on the distribution you are using and your preferences. We will look at installing from package repositories (apt, yum) and from source code.

Installing Git from Package Repositories (apt, yum)

This method is the easiest and recommended for most users. It allows you to install Git using the standard package management tools of your distribution. For Debian/Ubuntu:
sudo apt update
sudo apt install git
git --version # Check Git version
After installation, check the Git version to make sure the installation was successful. The command output should contain the installed Git version number. For example:
git version 2.34.1
For CentOS/RHEL:
sudo yum update
sudo yum install git
git --version # Check Git version
Similarly to Debian/Ubuntu, check the Git version after installation. For Fedora:
sudo dnf update
sudo dnf install git
git --version # Check Git version
And again, make sure Git is installed correctly by checking the version.

Installing Git from Source Code

Installing from source code gives you more control over the installation process, allowing you to use a specific version of Git or configure build parameters. However, this method requires more time and knowledge. Step 1: Download the Source Code Go to the official Git website and download the latest version of the source code. You can copy the download link and use the `wget` command on your VPS.
wget https://github.com/git/git/archive/v2.34.1.tar.gz # Replace with the current version
tar -xvzf v2.34.1.tar.gz # Unpack the archive
cd git-2.34.1 # Go to the source code directory
Replace «v2.34.1» with the downloaded Git version number. Step 2: Building and Installing Before building, make sure you have the necessary development tools installed. (They are listed in the «Preliminary VPS Preparation» section). Then run the following commands:
./configure --prefix=/usr/local
make
sudo make install
The `—prefix=/usr/local` parameter specifies where Git will be installed. You can change it if you want to install Git in a different location. Step 3: Checking the Installation After installation, add `/usr/local/bin` to the `PATH` variable so that Git is available from the command line. Edit the `~/.bashrc` file (or a similar configuration file for your shell):
nano ~/.bashrc
Add the following line to the end of the file:
export PATH="$PATH:/usr/local/bin"
Save the file and reload the shell configuration:
source ~/.bashrc
Now check the Git version:
git --version
If everything is done correctly, you will see the installed Git version number.

Installing from source code gives you complete control but requires more experience. If you are a beginner, it is better to use the installation from package repositories.

Elena Sidorova, Developer
Installation MethodAdvantagesDisadvantages
Package RepositoriesEasy to install, automatic updatesLimited choice of versions, dependency on repositories
Source CodeFull control, ability to customizeInstallation complexity, requires more time

Configuring Git After Installation

After installing Git, you need to configure its basic parameters, such as username, email address, and default editor. These parameters are used when creating commits and allow you to identify the author of the changes.

Configuring Username and Email Address

The username and email address will be associated with your commits. Set them globally so that they apply to all your Git repositories.
git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"
Replace «Your Name» with your name and «your_email@example.com» with your email address. You can check the configured settings using the command:
git config --global user.name
git config --global user.email
This command will display the configured name and email values.

Configuring the Default Editor

Git uses the default editor to enter commit messages, resolve conflicts, and perform other operations. You can choose any text editor that is convenient for you. Examples: To use Nano:
git config --global core.editor "nano"
To use Vim:
git config --global core.editor "vim"
To use VS Code (if it is installed and available from the command line):
git config --global core.editor "code --wait"
Check the installed editor:
git config --global core.editor

Configuring Color Scheme

Configuring the color scheme improves the readability of Git output. It is recommended to enable the color scheme for all commands.
git config --global color.ui true
Now the Git command output will be displayed using different colors.

Configuring File Ignoring

It is often necessary to exclude certain files and directories from Git tracking, for example, configuration files, temporary files, or compiled code. The `.gitignore` file is used for this. Create a `.gitignore` file in the root of your repository and list the files and directories you want to ignore in it. Example of a `.gitignore` file:
# Ignore configuration files
config.ini
*.log

# Ignore temporary files
tmp/*
*.swp

# Ignore compiled code
*.o
*.exe
You can create a global `.gitignore` file (applies to all repositories) like this:
git config --global core.excludesfile ~/.gitignore_global

nano ~/.gitignore_global
And add ignore rules to `~/.gitignore_global`. Important: Files that are already tracked by Git will not be ignored, even if they are specified in `.gitignore`. To exclude them, you must first remove them from the Git index using the `git rm —cached ` command.

Configuring Command Aliases

Aliases allow you to shorten long Git commands and simplify their use. You can create aliases for frequently used commands. Examples: To create an alias for the `git status` command:
git config --global alias.st status
Now you can use the `git st` command instead of `git status`. To create an alias for the `git log —oneline —graph —decorate` command:
git config --global alias.lg "log --oneline --graph --decorate"
Now you can use the `git lg` command to display the commit history in a convenient format. The full list of settings is stored in the `~/.gitconfig` file.
cat ~/.gitconfig
Example file content:
[user]
 name = Your Name
 email = your_email@example.com
[core]
 editor = nano
 excludesfile = /home/gituser/.gitignore_global
[color]
 ui = true
[alias]
 st = status
 lg = log --oneline --graph --decorate

Creating and Using a Git Repository

After installing and configuring Git, you can start creating and using Git repositories. We will look at creating a local and remote repository, adding files, commits, branches, and other basic operations.

Creating a Local Repository

To create a local repository, go to your project directory and run the `git init` command.
cd /var/www/myproject
git init
This command will create a hidden `.git` directory in your project, which contains all the repository information.

Adding Files and Creating Commits

After creating the repository, you can add files to the Git index using the `git add` command.
git add . # Add all files in the current directory
git add filename.txt # Add a specific file
Then create a commit using the `git commit` command. A commit is a snapshot of your project’s state at a particular point in time.
git commit -m "Initial commit" # Create a commit with the message "Initial commit"
The commit message should be short and informative, describing the changes you made.

Working with Branches

Branches allow you to develop new features or fix bugs without affecting the main codebase. Create a new branch using the `git branch` command.
git branch feature/new-feature # Create a branch named "feature/new-feature"
Switch to the new branch using the `git checkout` command.
git checkout feature/new-feature # Switch to the "feature/new-feature" branch
Now you can make changes to the new branch and create commits. After you’re done working with the branch, you can merge it with the main branch (usually `main` or `master`) using the `git merge` command.
git checkout main # Switch to the main branch
git merge feature/new-feature # Merge the "feature/new-feature" branch with the main branch
If conflicts arise during the merge, you will need to resolve them manually.

Working with a Remote Repository

«` A remote repository is a repository located on a remote server, such as GitHub, GitLab, or Bitbucket. It allows you to collaborate with other developers and store backups of your code. To connect to a remote repository, use the `git remote add` command.
git remote add origin git@github.com:your_username/your_repository.git # Connect to a remote repository
Replace «git@github.com:your_username/your_repository.git» with the URL of your remote repository. «origin» is an alias for the remote repository. You can use any other name. To send local changes to a remote repository, use the `git push` command.
git push origin main # Send the "main" branch to the remote repository "origin"
To retrieve changes from a remote repository, use the `git pull` command.
git pull origin main # Get changes from the "main" branch of the remote repository "origin"
The `git clone` command allows you to clone an existing remote repository to your local machine.
git clone git@github.com:your_username/your_repository.git # Clone the repository
Example: Let’s develop a simple Python script, add it to a Git repository, make a commit, and send it to the remote repository. First, create a `hello.py` file:
nano hello.py
Add the following code:
print("Привет, мир!")
Add it to Git then:
git add hello.py
git commit -m "Added hello.py"
git push origin main
For the first `push` you may need to specify `—set-upstream`:
git push --set-upstream origin main

Setting up a Git Server (optional)

Instead of using public services such as GitHub or GitLab, you can set up your own Git server on your VPS. This gives you complete control over your repositories and allows you to store confidential code on your own infrastructure. We will look at setting up a Git server using SSH, Gitolite, and Gitea.

Setting up a Git Server using SSH

The easiest way to set up a Git server is to use SSH. This method is suitable for small teams and projects where a complex access control system is not required. Step 1: Creating a Repository Create a directory to store Git repositories. For example:
sudo mkdir /var/git
sudo chown gituser:gituser /var/git
chmod 777 /var/git
cd /var/git
Create a Bare repository:
git init --bare myproject.git
Step 2: Cloning the Repository to a Local Machine Clone the repository to your local machine using the `git clone` command.
git clone gituser@your_vps_ip_address:/var/git/myproject.git
Replace «gituser» with the username you created, and «your_vps_ip_address» with the IP address of your VPS. Step 3: Sending Changes to the Server After making changes to the repository on your local machine, you can send them to the server using the `git push` command.
git push origin main
This method is easy to set up but does not provide advanced access control. Every user who has access to the `gituser` account can modify all repositories.

Setting up Gitolite

Gitolite is a more advanced way to set up a Git server that provides a flexible access control system. Step 1: Installing Gitolite Log in to the VPS as the `gituser` user and clone the Gitolite repository.
git clone https://github.com/sitaramc/gitolite.git
mkdir -p ~/bin
mv gitolite ~/bin/
Then run the Gitolite installer.
~/bin/gitolite install
Step 2: Configuring Gitolite Run Gitolite using your public SSH key.
gitolite setup -pk ~/.ssh/id_rsa.pub
Replace `~/.ssh/id_rsa.pub` with the path to your public SSH key. Step 3: Managing Repositories Now you can manage repositories and access rights using the `gitolite-admin` repository. Clone it to your local computer.
git clone gituser@your_vps_ip_address:gitolite-admin
The `gitolite-admin` repository contains two directories: `conf` and `keydir`. The `conf` directory contains repository configuration files, and the `keydir` directory contains users’ public SSH keys. To create a new repository, add a configuration file to the `conf` directory and the user’s SSH key to the `keydir` directory. Then send the changes to the `gitolite-admin` repository. Example configuration file `conf/gitolite.conf`:
repo myproject
    RW+ = @all
This configuration grants read and write access to all users. You can configure more complex access rules using user groups and different access rights.