Digital Media Insights for Business Owners

Setup a Git Repository in your VPS

Git Repo on your VPS

When I was new to the whole version control scene, I wanted to jump in the wagon with both feet and really start using GIT. The problem was that it is not as easy as people make it sound. I was confused with figuring out what a repository meant, what pushing, cloning and all sorts of problems that a beginner may have. And the problem is that the documentation available was also a too technical and difficult to read.

That’s why I wanted to explain it here. Now, my personal challenge was that since I have access to a VPS via SSH, I didn’t want to necessarily pay for a private repository if I didn’t have to. And since I was beginning to use GIT, I really didn’t need a great account with Github or something along those lines.

The other challenge was that I wanted my repository to be available everywhere but also to be private to me. I wanted to be the only one to access it. I wanted to be able to work remotely in projects from different machines and keeping the code on a VPS repository while I was developing.

So my solution was: I needed to learn how to setup a git repository in a VPS. After spending a good few days reading articles and all sorts of things, I have condensed the best articles and added my own view to make it friendlier. At first, it was for me to remember how to do it, but eventually it was also for you guys out there wanting to learn how to do this.

Here we go.

Accesing the VPS without a Password

1. Create a user for git

First, it is recommended that you create a user in your VPS that you use specifically for git. You probably know already how to create a user in your VPS, and I am not going to go into details on how to do that part. Remember that if you have a domain registered in that VPS, you already have a user that administrates that domain in CPANEL, so you could use that. The main idea here is to not use root for this.

2. Create RSA keys in your computer.

Creating RSA keys allows you to access your VPS remotely without having to enter a password. The process is fairly simple. First, log into the terminal in your local computer (the one that is going to hold your keys) and generate the key:

					$ cd ~/.ssh
$ ssh-keygen -t rsa -C “”

Now, in your .ssh directory on your local computer you should have two new files one public and one private. Those are your keys. You can add your keys by using cPanel and accessing your SSH section, importing your public key and authorizing your access or you can append your public key to the authorized keys file in your root ssh directory on your VPS. So let’s do that now.

3. Copy the public key to the server

					/* Secure Copy the RSA Public Key to the Server (VPS) */
$ scp ~/.ssh/ gituser@server_ip:./

/* Log into the VPS via SSH using your user */
$ ssh gituser@server_ip 

/* (Append using command "cat" the contents of your file to the authorized_keys) */
$ cat >> /home/gituser/.ssh/authorized_keys

Notice the “>>” this means concatenate, this is important because if you were to use “>” instead, you would remove what the file has and replace it with your RSA key.

4. Now you can access your server without needing a password – you can test this by using:

					$ ssh user@yourserver_ip 

In some servers, the port is not the default SSH port, so you may have to also include the port number with the flag -P followed by the port number.

5. You can exit the VPS for now:

					$ exit


A repository is where all your files are stored. In Git terminology, a bare repository is one that is empty. It doesn’t add or commit anything, but that receives its contents from a push.

In other words, think of it as a bucket in the roof of your house. It only gets water when it rains. If you had a bucket in the ground, you could put water on it with a glass or a hose, etc; that would be a local git repository that accepts “add” and “commit”.

1. Creating your first repository in the VPS.

Log into your VPS via SSH.

					$ ssh gituser@yourserver_ip

2. Create a directory where you are going to store your files (it is a convention to name this directories with .git a the end)

					$ mkdir my_first_repo.git 

3. Log into that directory

					$ cd my_first_repo.git

4. Use the git init command (The bare argument is what makes it the bucket in the roof)

					$ git --bare init

5. That’s it in the server. Make sure that you note the location where you created the repository. You can find out by running “pwd”

6. Exit

					$ pwd
$ exit

Downloading (Cloning) your Repo files to your local machine

OK, great. So now we need to get the contents (for now is empty) of your repository to your local machine.

1. Open up terminal in your local machine.

2. Change directory to the directory where you keep your projects.

					$ cd Documents/

3. Clone your project files from the repository.

Of course, change the location to the location that you found out on item 5 of the previous section. Remember that you used “pwd”? Use that location after the “:”

					$ git clone gituser@yourserver_ip:/gituser/my_first_repo.git

4. That’s it, now GIT goes to your VPS, grabs a fresh new copy of your files (which you have none so far) and comes back and first creates a folder for you. Then, it downloads a copy of all your files creating an effective “clone” of your repository in the VPS.

Start using Git to track all your goodies locally

Cool, now let’s go ahead and start using Git for your project.

1. Let’s say that you are actually creating a simple txt file.

2. Go to your text editor and create the new txt file, and then save it – where? Simple, to the directory that we created in the previous section.

3. Ok, so now our directory is not empty it has this “samplefile.txt”. But this file is only in your machine. It is not even inside of your local git repository yet. It is just in the git local directory.

4. Now you want to add this file to your local repository. First you add it and then you commit it.

Notice that the commit command also has a “-m” flag, this means message, enter a message that describes the commit.

You have two repositories. One of them is in your VPS, the other one is in your local machine. So we are going to start by adding the txt file to your local repository and then running commit.

					$ git add samplefile.txt
$ git commit -m "first commit"

5. Nice, so now, that file is considered commited. Cool, but remember that is just locally, so how do we really push it to the bucket in the roof?

Push your local changes to the Repository in the VPS

How do we send it to the repository in the VPS? Simple,the command is “push”.

					$ git push origin master

The repository’s name is origin and the branch is the master branch. That’s why we add the arguments: origin master.

Now, your repository has the lastes changes, which now includes a file called samplefile.txt.

So if you are in a different computer, you can simply clone the repo just as before and work on the file as you would normally.

Once finished, you would add the file or files or directories, and then commit them. As a final step you would push the changes to the VPS.

What if you already have cloned this on your machine? Then instead of cloning again, what you do is a “pull”. A pull simply grabs the latest commits from the repository and branch(in this case origin master) and updates your local repo.

					$ git pull origin master

That’s it. So now you have a working private VPS Repo – that’s great stuff.

Say: "Hola" to your new clients.

Follow us on Social Media for more Tips & Tricks.

Other Posts You May Enjoy