How to Setup a Git Server
4 min read
When it comes to Git hosting, you have a number of options available. GitHub, Gitlab and Bitbucket are popular solutions, but running your own Git server is an alternative worth considering.
Setting up a Git Server allows you to create private repositories without the restrictions of the providers’ free plans.
In this guide, we explain how to set up a bare Git server on Linux. This setup is good if you have few repositories and the collaborators are tech-savvy. Otherwise, you should consider installing a self-hosted git application such as Gitea, Gogs, or Gitlab.
The Git server can be set up on any remote Linux machine or even on your local system.
Setting Up the Git Server
The first step is to install Git on your server.
If you are using Debian or Ubuntu, refresh the local package index and install git by running the following commands as sudo user:
sudo apt update && sudo apt install git
To install the git package on CentOS servers type:
sudo yum install git
Next, create a new user that will manage the Git repositories:
sudo useradd -r -m -U -d /home/git -s /bin/bash git
The user home directory is set to
/home/git. All the repositories will be stored under this directory. We did not set a password for the user “git”, the login will be possible only using the ssh keys.
Switch to user “git” using the
sudo su - git
mkdir -p ~/.ssh && chmod 0700 ~/.ssh
Create a file named
~/.ssh/authorized_keys which will hold the authorized users’ SSH keys:
touch ~/.ssh/authorized_keys && chmod 0600 ~/.ssh/authorized_keys
That’s it. The server setup is complete. You’re now ready to create your first Git repository.
Run the following command to initiate a new empty repository:
git init --bare ~/projectname.git
You can name the directory as you want. The important thing is to create the repository under the “git” user home directory.
Initialized empty Git repository in /home/git/projectname.git/
Configuring Local Git Repository
To be able to push the local git changes to the Git server you’ll to add your local user SSH public key to the remote “git” user’s
If you already have an SSH key pair created on your local system, you can display the public key by typing:
The output should look something like the following:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDd/mnMzHwjUzK8g3ldfsfRpJuC16mhWamaXRk8ySQrD/dzpbRLfDnZsLxCzRoq+ZzFHGwcQlJergtergdHGRrO8FE5jl3IWRRp+mP12qYw== firstname.lastname@example.org
If you get an error message saying
No such file or directory, it means that you do not have an SSH key pair generated on your local machine.
To generate a new SSH key pair use the following command:
ssh-keygen -t rsa -b 4096 -C "email@example.com"
Copy the output from the
cat command above and go back to the Git server console.
On the server, open your text editor and paste the public key that you copied from your local machine into the
sudo nano /home/git/.ssh/authorized_keys
The entire public key text should be on a single line.
We’re assuming that Git package is already installed on your local machine. If not, install it in the same way as explained in the previous sections.
If you have an existing unversioned project, navigate to the project directory. If you are starting from scratch, create the project directory, and navigate to it:
Initialize a git repository:
git init .
The last step is to add the git remote to your local repository:
git remote add origin git@git_server_ip:projectname.git
Do not forget to replace
git_server_ip with your Git server hostname or IP address.
To verify that everything is setup up correctly, create a test file:
Add the changes to the staging area:
git add .
Commit the changes:
git commit -m "descriptive message"
Push the local repository changes to a remote repository:
git push -u origin master
If everything is set up correctly, the output should look something like this:
Counting objects: 3, done. Writing objects: 100% (3/3), 218 bytes | 218.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To git_server_ip:projectname.git * [new branch] master -> master Branch 'master' set up to track remote branch 'master' from 'origin'.
To add a new collaborator, just copy its public SSH key to the “git” user’s
You can use the same steps to create new repositories. It is important to note that the remote repository must exist before you add the git remote to your local repository.
In this tutorial, we have shown you how to set up your own private Git server and create repositories.
If you hit a problem or have feedback, leave a comment below.