August 8, 2013


How To Set Up Automatic Deployment with Git with a VPS

Tagged In: Git


For an introduction to Git and how to install, please refer to the introduction tutorial.

This article will teach you how to use Git when you want to deploy your application. While there are many ways to use Git to deploy our application, this tutorial will focus on the one that is most straightforward. I assume you already know how to create and use a repository on your local machine. If not, please refer to this tutorial.

When you use Git, the workflow generally is toward version control only. You have a local repository where you work and a remote repository where you keep everything in sync and can work with a team and different machines. But you can also use Git to move your application to production.

Server Setup

Our fictitious workspace:
Your server live directory: /var/www/
Your server repository: /var/repo/site.git

What should we do if we want to push to site.git and at the same time make all the content available at /var/www/

Creating Our Repository

Login to your VPS from command line and type the following:
cd /var
mkdir repo && cd repo
mkdir site.git && cd site.git
git init --bare

--bare means that our folder will have no source files, just the version control.


Git repositories have a folder called 'hooks'. This folder contains some sample files for possible actions that you can hook and perform custom actions set by you.

Git documentation define three possible server hooks: 'pre-receive', 'post-receive' and 'update'. 'Pre-receive' is executed as soon as the server receives a 'push', 'update' is similar but it executes once for each branch, and 'post-receive' is executed when a 'push' is completely finished and it's the one we are interested in.

In our repository if you type:

You will see a few files and folders, including the 'hooks' folder. So let's go to 'hooks' folder:
cd hooks

Now, create the file 'post-receive' by typing:
cat > post-receive

When you execute this command, you will have a blank line indicating that everything you type will be saved to this file. So let's type:
git --work-tree=/var/www/ --git-dir=/var/repo/site.git checkout -f

When you finish typing, press 'control-d' to save.

In order to execute the file, we need to set the proper permissions using:
chmod +x post-receive

You can see on the documentation that 'git-dir' is the path to the repository. With 'work-tree', you can define a different path to where your files will actually be transferred to.

The 'post-receive' file will be looked into every time a push is completed and it's saying that your files need to be in /var/www/

Local Machine

Let's create our local repository. You should change the path and name to whichever you choose. If you are on a VPS, just type:

And create your repo:
cd /my/workspace
mkdir project && cd project
git init

Then we need to configure the remote path of our repository. Tell Git to add a remote called 'live':
git remote add live ssh://[email protected]/var/repo/site.git

Here we should give the repository link and not the live folder.

Let's assume that we have some great work ready in this folder. We should do the usual steps of adding the files and commit with a message:
git add .
git commit -m "My project is ready"

Just to remember, the dot after 'git add' means you are adding all files to stage. After 'git commit' we have '-m' which means we will type a message. To complete, we just 'push' everything to the server. We use the 'live' alias that we used when setting the remote.
git push live master

Counting objects: 7, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (7/7), 10.56 KiB, done.
Total 7 (delta 0), reused 0 (delta 0)
To ssh://[email protected]/var/repo/site.git
* [new branch] master -> master

Here we tell Git to push to the 'live' remote on the 'master' branch. To understand more about branches and how to use it you can read this tutorial.


What if you don't want to deploy everything in one step? Maybe you want to test it first and have a beta directory.

One of the ways to do that is create another repository. Let's log in again in our VPS and create our directory:
cd /var/www/
mkdir beta

To create our repository:
cd /var/repo
mkdir beta.git && cd beta.git
git init --bare

Again we should create the 'post-receive' file because we want to see our project in the beta directory:
cd hooks
cat > post-receive

Type the content of the file:
git --work-tree=/var/www/beta --git-dir=/var/repo/beta.git checkout -f

Press 'control-d' to save.

Let's go back to our local repository:
cd /my/workspace/project

So now we can set another remote pointing to our beta repository:
git remote add beta ssh://[email protected]/var/repo/beta.git

With this, we can have a two step process. First we push to beta and check, and if everything is fine we push to live:
git add .
git commit -m "New version"
git push beta master

And later:
git push live master

Going Live From the Server

Maybe you have a team working in the same project and you want that others can also decide that it's time to go live. To do this, we can link the beta and live repository on the server. Log in to your VPS and type:
cd /var/repo/beta.git
git remote add live ../site.git

So now you can push from beta to live on the server:
cd /var/repo/beta.git
git push live master

Congratulations! Your VPS is now set to automatically deploy with Git!
Submitted by: Caio Vaccaro

Share this Tutorial

Vote on Hacker News

Try this tutorial on an SSD cloud server.

Includes 512MB RAM, 20GB SSD Disk, and 1TB Transfer for $5/mo! Learn more

Create an account or login:


Write Tutorial
  • Gravatar maxcnunes 6 months

    In all examples you are accessing the repository in "~/var", when I think should be only "/var". Is that right?

  • Gravatar oscar.psu 6 months

    You guys are the best! Thanks for all this info

  • Gravatar Kamal Nasser 6 months

    @maxcnunes: It should be ~/var, I've updated the article.

  • Gravatar Kamal Nasser 6 months

    My bad, should be /var.

  • Gravatar maltesepiero 5 months

    Thank you. Very useful. Likewise, how could I cancel latest deploy?

  • Gravatar chrisyeung 5 months

    Received the following error.. I have followed every step of the tutorial... fatal: '/var/repo/site.git' does not appear to be a git repository fatal: Could not read from remote repository.

  • Gravatar chrisyeung 5 months

    Solution found, Reference from: when adding the git remote. I changed ssh://user@domain/var/repo/site.git to ssh://user@domain/root/var/repo/site.git It works now.

  • Gravatar Kamal Nasser 5 months

    @maltesepiero: See

  • Gravatar mikav 5 months

    Great tutorial, but I like to have my webservers as "clean" as possible. Would something like this be possible if the git repos and the website would be on different vps?

  • Gravatar Kamal Nasser 5 months

    @mikav: I don't recommend moving the repo to a separate droplet simply because there is no need to do that. You can modify the script to rsync the website's files to the webserver droplet however this isn't covered in this article.

  • Gravatar Dave Gafni 4 months

    At the step "cat > post-receive" I got "-bash: post-receive: Permission denied" Same with "sudo cat > post-receive. Instead I did "sudo nano post-receive" and typed the next step in. Not surprisingly, when I did "git push live master" locally, I got: error: insufficient permission for adding an object to repository database ./objects fatal: failed to write object error: unpack failed: unpack-objects abnormal exit Suggestions? Thanks.

  • Gravatar marco.agner 3 months

    I think I'm getting some kind of permission error. This is what I got: fatal: Unable to create temporary file '/var/repo/site.git/./objects/pack/tmp_pack_XXXXXX': Permission denied error: unpack failed: index-pack abnormal exit To ssh://[email protected]/var/repo/site.git ! [remote rejected] master -> master (n/a (unpacker error)) error: failed to push some refs to 'ssh://[email protected]/var/repo/site.git' Does anybody have suggestions for this problem? Thank you for the article.

  • Gravatar marco.agner 3 months

    Forget it. I have found a solution. I was pushing to an user that couldn't write the files. Thank you.

  • Gravatar sajuna19 3 months

    I get this error after following the tut through. Great tut btw. remote: fatal: cannot exec 'hooks/post-receive': Text file busy

  • Gravatar Kamal Nasser 3 months

    @sajuna19: What's the output of

    lsof | grep post-receive
    ? Rebooting the droplet usually fixes it.

  • Gravatar Francisco Calle Moreno 3 months

    Does the hook also remove elements?

  • Gravatar Kamal Nasser 3 months

    @Fransisco: What do you mean by "remove elements"?

  • Gravatar amrta.kf about 1 month

    Hi there, First of all, thanks for the helpful tutorials. However, I am stuck with the tutorial documented in I have followed all the steps: * create directory in live server for both repo and live directory * run command git init --bare * create post-receive hook with the following code #!/bin/sh git --work-tree=~/var/www/ --git-dir=~/var/repo/site.git checkout -f (I also tried "git --work-tree=/var/www/ --git-dir=/var/repo/site.git checkout -f" but no different result) * chmod +x post-receive * push project from local machine After doing all that, I checked the /var/www/ but there is no file or directory added here after git push. However, I checked the commit in /var/repo/site.git/refs/head and there is master file and when I run command cat master it point to the correct commit key Just to clarify, did I do something wrong?

  • Gravatar amrta.kf about 1 month

    found a way, it turned out that I need to use absolute path in the git hook: #!/bin/sh git --work-tree=/root/var/www/ --git-dir=/root/var/repo/site.git checkout -f I'm not sure though why the suggested steps in the tutorial didn't work out for me.

  • Gravatar ty about 1 month

    I have to repeatedly enter my ssh password for my user every time I push live. How do add ssh keys in this type of workflow so I don't have to enter password every time?

  • Gravatar Kamal Nasser about 1 month

    @ty: See

  • Gravatar ingro85 about 1 month

    Hello and thanks for this nice tutorial! I found a problem anyway, when I hit "git push" on my local machine the remote repo is updated correctly, anyway the working tree directory isn't created and I got this message: - remote: fatal: This operation must be run in a work tree Which for what I know about git is legit. So how did you make it works with bare repos? Am I missing something? Thank you!

  • Gravatar david.l.vergara 16 days

    Hello and thanks for this tuto, but i get this error: "error: cannot run hooks/post-receive: No such file or directory" i dont know what to do, help please!

  • Gravatar kenny.lee28 16 days

    I followed the instructions and had problems pushing to remote. I checked out git's documentation and turns out a colon (:) is needed between the domain address and path when adding the remote. For example: git remote add remotedroplet master ssh://username@address:/path/to/git/repository

  • Gravatar owner 12 days

    Hi, Was able to fix the issue with - remote: fatal: This operation must be run in a work tree by creating the folder in the /var/www - e.g. mkdir /var/www/ before you can actually do the git --work-tree=~/var/www/ --git-dir=~/var/repo/site.git checkout -f Hope this helps.

  • Gravatar max.pittsley 7 days

    I had a problem where my files were not being properly written. When I tried to git push, I was getting git's usage statement back from the server. It turns out that if you copy and paste the post-receive script from this webpage into nano, a line-break appears right before "checkout -f" for some reason. That's what causes this type of error. If you run into the same thing, just make sure that entire command is on one line in the bash file.

Leave a Comment

Create an account or login: