LowEndBox - Cheap VPS, Hosting and Dedicated Server Deals

Setting up Git for Free on a VPS

lowendtutorial

Have you thought about setting up your own private Git server on a spare VPS?

Thanks to the popularity of services like GitHub, Git has become very widespread among the developer community. You don’t have to use a service like GitHub to enjoy the benefits of git. It is open source and available to everyone.

There are a lot of reasons that you might want to run your own git server. Primarily, because many other developers understand it and won’t have to face the learning curve of your version control system to contribute to your project. Let’s go over how to set up git on your VPS.

Prerequisites for Setting Up Git on your VPS

We will be using SSH keys for authentication and we will not use the web interface.

In case you need a web interface to access your repositories, some options for you might be Gitosis, GitList and Goblet.

First, we need to generate a SSH key pair. If you are using Mac or Linux, you can simply issue the following command in a terminal:

> ssh-keygen

If you are using Windows based operating system, there are tools available to generate key pairs, such as PuTTY Gen.

Once we have our keys, we can log into our VPS and create a user specifically for Git. We are going to name it “git”, but feel free to name it whatever you’d like.

Log into your VPS, and gain root privileges with the following command:

> su -

Or if you prefer you can use sudo commands on each of the following. We now need to add the user to handle the repositories:

> useradd git

And then give it a password:

> passwd git

The next step is to install the git server using the following command depending which distro you are using on your VPS:

> yum install git (CentOS/Fedora)

> apt-get install git (Ubuntu/Debian)

We will now need to add the SSH key we previously generated to the git user. We will therefore need to be logged in as the git user. You can easily switch using this command:

> su git

We will now need to upload the public SSH key contained in the “id_rsa.pub” file to your Git user’s home directory. You can easily do that using the following commands from your workstation in case you are using a Mac or Linux based system:

> cd home-dir/.ssh

> scp id_rsa.pub git@VPS-IP-ADDRESS:/home/git/id_rsa.pub

The second command will ask you for the password of the git user we have just created.

If you are on a Windows based system, locate the file and use WinSCP to upload it in the home directory on your VPS.

Once you have done that, we will need to add it to the authorized keys file. To do that use the following commands:

> mkdir ~/.ssh

> touch ~/.ssh/authorized_keys

> cat ~/id_ras.pub >> .ssh/authorized_keys

Remember to always use the >> modifier, because using the single > will overwrite the content of the file, while the double simply appends content at the end of the file.

What if you need to add more keys? Just upload the file as we did earlier and run the last command. Each line in this file is an entry for a key that you wish to have access to this account.

Let’s now set up a local repository. In order to do so we will need to create a folder as an empty git repository using the following command:

> git init --bare new-repo.git

Let’s move on to how to use it with your local computer. On Linux or Mac, you’ll need to change the remote origin to your newly created server.

If you already have a local repo that you want to push to the server, change the remote using this command:

> git remote set-url origin git@VPS-IP-ADDRESS:new-repo.git

If this is a new repository, use this command instead:

> git init && git remote add origin git@ VPS-IP-ADDRESS:new-repo.git

On a windows client you will just need to configure the preferences of your GIT client or add a new repository.

Now you will be able to work knowing that your code is only accessible to yourself!

Jon Biloh

18 Comments

  1. Much nicer formatting!

    Well done.

    Francisco

    October 5, 2016 @ 9:34 pm | Reply
  2. Thanks! We’re going to make all future tutorials follow this type of formatting. :)

    October 5, 2016 @ 9:57 pm | Reply
  3. Yes, this one more readable for me :D

    October 6, 2016 @ 3:57 pm | Reply
  4. joepie91:

    “An example of what you could do with the free version of Git […]”

    What? Git and GitHub are not the same thing…

    October 6, 2016 @ 8:25 pm | Reply
    • fooPlus:

      Just what I thought … next, someone will write about the free version of Apache?

      I mean, it’s not only not the same thing, it’s even kindof the reverse of reality: Git was created as free software by Linus Torvalds, and then lateron Github came along and used that existing free software to build their business using it.

      October 6, 2016 @ 9:35 pm | Reply
  5. chris s:

    If you want a webified version of git for yourself, you can buy a perpetual license for bitbucket for 1-10 users for $10 (you have to pay if you want support, but most people won’t need that).

    October 6, 2016 @ 10:53 pm | Reply
    • Anonym:

      Or you can just enjoy the unlimited and fully free “Community Edition” of GitLab…

      October 8, 2016 @ 3:33 pm | Reply
    • niknah:

      bitbucket also has free unlimited private repositories.

      October 9, 2016 @ 3:42 am | Reply
  6. tmwc:

    You could also use Gogs.

    Pretty much just download Gogs, run it in screen/tmux/whatever (on user git :P), and put nginx on port 80/443.

    October 7, 2016 @ 12:21 am | Reply
  7. Sven:

    Just use Gitlab.

    October 7, 2016 @ 5:57 pm | Reply
    • bakhtiyor:

      gitlab is very heavy, better to use gogs is much lighter

      October 26, 2016 @ 1:18 pm | Reply
  8. c:

    + gitlab

    October 27, 2016 @ 11:47 am | Reply
  9. too many thanks for instaling info

    January 3, 2017 @ 4:45 am | Reply
  10. thanks for installing info

    August 31, 2017 @ 6:00 pm | Reply
  11. thanks dear , i used this.

    December 17, 2017 @ 4:40 am | Reply
  12. gitlab is very heavy

    December 17, 2017 @ 5:25 am | Reply
  13. very good thanks

    January 29, 2022 @ 12:04 pm | Reply

Leave a Reply

Some notes on commenting on LowEndBox:

  • Do not use LowEndBox for support issues. Go to your hosting provider and issue a ticket there. Coming here saying "my VPS is down, what do I do?!" will only have your comments removed.
  • Akismet is used for spam detection. Some comments may be held temporarily for manual approval.
  • Use <pre>...</pre> to quote the output from your terminal/console, or consider using a pastebin service.

Your email address will not be published. Required fields are marked *