How to Create Your Own Git Server

Set up your own git server, super easy | InMotion Hosting

There are so many Git hosting sites out there, including the popular GitHub, which acts as a hub for thousands of software projects. But for one reason or another, hosting your own Git repository may be preferable. In this article, you’ll learn exactly how to do that on your own VPS Hosting account. But any fast VPS hosting account will work just fine.

Why Create Your Own Git Server?

Improved security
GitHub, and other services, provide good protective parameters but accidents can happen, and have happened in the past. In general, having any sensitive data on third-party servers should be avoided where possible.
More control
Managing your Git server means you can customize and optimize the hosting environment for your needs specifically. Many of the premium options you get on your own server for free will cost you extra on a hosted service.
More options
There’s no reason why you can’t do a combination of both. If someone on your team wants to host their part of the project on GitHub, or another service, they’re free to do that. Git is decentralized. You can have multiple remote repository locations.

Managing SSH Keys

As mentioned above, your self-hosted Git installation is more secure than most services because your cloud VPS comes pre-loaded with significant SSH security measures.

Using SSH keys for interacting with Git means you don’t have to manage passwords. If you have a team member who wants to contribute to the project, you can simply upload their public key to the Git server. When their work is done, or they are no longer contributing to the project, all you have to do is remove their key and they will no longer have access to the server.

You can also use HTTP to access remote repositories, but this will require more customization on your cloud VPS, and may introduce security risks. (If you’re interested in using HTTP instead of SSH, contact our managed hosting team and they will help you configure this kind of access.)

How the Process Works

Here is the basic rundown of the Git server process:

  • Create a “git” user and group
  • Upload a public SSH key to the “git” user which will allow you (or others) to log in as the “git” user
  • Create a bare repository on the server
  • Add the remote repository (the one just created on the server) to a local Git project
  • Push changes to the remote repository

The Theory Behind the Git User Account

Why create a “git” Linux user? Can you just use “root”? The git user is required because we will be pushing content to our server using SSH. In order to log in with SSH, a user is required. You could use the root user, but this poses a security hazard. It’s considered a best practice to limit access to your root user account. By creating a specialized git user for this process, you could—if needed—open your project to more contributors without granting access to the root user account. The git user account only has access to its own files. This way, another contributor, logged in as git, cannot make changes to your server or engage in any unwanted activity.

It might seem like an unnecessary extra step, but rest assured it is very important to create this additional Linux user on your server to manage Git remotely.

File Path

Before you create your first remote Git repository, you need to decide what file path you will save your repositories to. You can save repositories to different file paths, but it’s generally easier to have all of them in one directory.

Creating the “Git” User

First things first, it’s time to create a “git” user account with which to do all your git-related work. Remember, you can title this user account anything you want. Naming the account “git” is merely a simple way to indicate what this user account was created to do.

Remember, as you are logging into your cloud VPS account, you are using the root user. If you are not using your root user, you will need to prepend these commands with “sudo.”

Create the Git user with the adduser command:

adduser git

Answer the questions as you are prompted, or press Enter to skip over them. These questions are not required. For the password, you can put virtually anything, because you will not be using passwords to log into this user. Remember that password authentication is not allowed on your server by default.

Now that you have created the git user, you can assume the role by using the su command:

su git

You will now need to create a .ssh directory in the “home” folder for the git user. In order to do that, you must make sure you are in the home directory, so run a cd command to make sure:

cd

Now, create the .ssh directory:

mkdir .ssh

Next, make the directory only accessible by the git user:

chmod 700 .ssh

Now you have an .ssh directory to hold your “authorized_keys” file, which is used to authenticate login requests. It’s time to create that file now:

touch .ssh/authorized_keys

And, likewise, adjust the privileges to make the file accessible only to the git user:

chmod 600 .ssh/authorized_keys

Now, you have the directory and file necessary to set up the SSH logins.

But before we move on, make sure that the “git” group is allowed to authenticate this way.

You can do that by editing your SSH configuration and adding “git” to the acceptable groups listed in the configuration file.

Uploading Keys to the Git User

When you hear about “uploading” or “managing” keys, you are basically appending these keys to the .ssh/authorized_keys file. You can put as many public keys as you need in this file, and there are a few different ways to get to them there.

One way is to simply copy and paste the key from your local computer to the server.

There is also an SSH command that will copy the key to the server for you:

ssh-copy-id -i ~/.ssh/key user@<hostname, IP address, or domain name>

Creating the “Bare” Git Repositories

Once you’ve decided which directory will house your remote repositories, you can start creating repositories first as directories ending with a .git extension. The extension is important, as it distinguishes the directories containing Git repositories from regular directories.

For example, to create a repository called “website,” create a directory like so:

mkdir website.git

Then, enter the directory with the cd command:

cd website.git

Once inside the directory, create a “bare” Git repository:

git init --bare

Now, when you add website.git as a remote repository (as you’ll see how below) Git will be able to recognize it.

Creating the --bare git repository basically means you are telling Git, “There is nothing in this repository yet, but I want this directory (website.git) to function as a remote repository.

Add Your Remote Repository

So far, you have created a git user account, a bare repository in the directory of your choice, and you have SSH key authentication set up for the git user.

Now, you just need to make your local Git installation aware of the new repository:

git remote add origin git@server:/path/to/website.git

Let us break this command a little bit.

The git remote add command initiates the addition of a repository. The “origin” part names the new repository. You can put any name you want, you don’t have to use “origin,” but it is merely a conventional naming scheme.

The rest of the command specifies the precise server file path to the bare Git repository you created.

The final step is to push your project to the new repository. For example, if you are pushing the “master” branch, the command will look like this:

git push origin master

That’s it! You now know how to create your own Git repository on a private server.

While you’re here, you may also be interested to know how you can “checkout” (or, basically copy) files from your repository to a specific directory on your server. This is a great way to publish files to a document root or specific directory on your website.

Learn more from our VPS Hosting Product Guide.

Upgrade to VPS Hosting for Peak Performance

Upgrade to InMotion VPS Hosting today for top-notch performance, security, and flexibility, and save up to $2,493 – a faster, stronger hosting solution is just a click away!

check markDedicated Resources check markNVMe SSD Storage check markHigh-Availability check markIronclad Security check markPremium Support

VPS Hosting Plans

CM
Christopher Maiorana Content Writer II

Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog.

More Articles by Christopher

6 thoughts on “How to Create Your Own Git Server

  1. Cool! so basically, every dev’s ssh public key is in the authorised file and they log in with the git user. simple. how would that work with logging who did what though. and How would you then host two different repositories with their own users.. i guess another user (git1)

    1. Christian S. – thanks for your question about how multiple users work with logging and the ability to host 2 different repositories with their own users. Each user has its own profile, so any records should be recorded based on the profile. In order to ho 2 different repositories, then yes, another repository would be created.

      1. You mean, another user (git2) should be created to host a second repository (repo2) and add the public keys of the clients accessing repo2 to the .ssh folder created in git2’s home directory?

      2. Hello Karthiga – There may be some confusion here as the original comment was basically two questions. Yes, if you are using 2 repositories, then you would add public keys. If you’re trying to have multiple users on ONE repository, then you may want to follow this link. It’s about setting up GIT for multiple developers: https://shapeshed.com/setting-up-git-for-multiple-developers/.

Was this article helpful? Join the conversation!