Setup a Git Repository in your VPS

November 6th, 2012 admin Location: Raleigh, NC, Topic: Web Design | Digital Media

So I am pretty new to the whole version control scene. Therefore, 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, clone and all sorts of problems that a beginner may have. And the problem is that the documentation available is also a bit 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 is 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. The reason is that sometimes I am out of the country, and I would love to be able to simplify the process of working remotely in projects and keeping them on a VPS repository.

So my solution was: I need 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 verbose to make it friendlier. First, for me to remember how to do it, but of 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 for specifically 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.

2. Create RSA keys

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 “mail@domain.com”

Now, in your .ssh directory you have two new files one public and one private. Those are your keys. You need to append your public key to the authorized keys file in your VPS. 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/id_rsa.pub gituser@server_ip:./ 

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

/* (Append using command "cat" the contents of your rsa.pub file to the authorized_keys) */
$ cat id_rsa.pub >> /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 

You should see that you are logged into the VPS.

5. You can exit the VPS for now -> Exit

$ exit

CREATING YOUR REPOSITORY

A repository is where all your files are stored. In Git terminology, a bare repository is one that doesn’t add or commit anything, but that receives its contents from 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 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 command 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”

$ pwd

6. Exit

$ 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 goes ahead and 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 that contains the copy for your a new page on your site. So let’s create it.

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”.

4. Now you want to add this file to your repository. So 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.

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

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

Push your 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

Now, your repository has the last 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.

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

About the Author

Alex Centeno MBA., is an international creative director and digital media strategist. Currently residing minutes away from the Research Triangle Park (RTP) - North Carolina, and with over 10 years of interactive marketing training and experience, Alex leads Merkados' international clients to maximizing their online business strategies. One of Alex's biggest strengths is his world-class capacity to effectively combine interactive marketing, digital media design and web development.

You may find further information about Alex Centeno MBA. at: and Twitter.

Sobre el Autor

Alex Centeno MBA., Es un director creativo y estratega de medios digitales reconocido internacionalmente. En la actualidad reside a tan solo minutos del Research Triangle Park (RTP) - Carolina del Norte, y con más de 10 años de entrenamiento y experiencia en marketing interactivo, Alex lidera a los clientes internacionales de Merkados™ para maximizar sus estrategias de negocios en línea. Una de las mayores fortalezas de Alex es su capacidad global de combinar de manera efectiva, mercadeo interactivo, diseño de medios digitales y desarrollo web.

Para más información sobre Alex Centeno MBA. puede visitar: y Twitter.