In this video you will learn the fundamentals of using Git - specifically a git push - to deploy your code to a server.

This method is particularly useful in a development environment. For production deployments, something a little more robust would be advised.

The basic concept of this method is to enable you to run a single command, e.g.

git push server master

Which will push your 'master' branch up to your 'server', and then take advantage of the post-receive hook file to run any additional commands once the code has been successfully received by the server.

This is particularly useful with Symfony projects whereby you may wish to clear caches, run assetic:dump or what have you whenever you put new code changes on your development server.

If you want to follow along with the handy command line shortcuts, be sure to check out ohmyzsh.

How To

This guide assumes you are starting from fresh. If you have an existing server, this should be relatively easy to adapt as required.

All this will take place in your home directory, but of course, can be run from other directories, as long as you have the relevant permissions.

Start by creating two new directories:

mkdir git
mkdir deploy

You will then need to create a bare git repository.

cd git
mkdir example.git
cd example.git
git --bare init

This will initialise an empty Git repository in the example.git directory. As part of this, you will gain a hooks directory.

Change into that directory:

cd hooks

Then we need to create a file called post-receive and make it executable:

touch post-receive
chmod +x post-receive

Making sure the file is executable is very important, otherwise most of this will work, but your post-receive script will seemingly inexplicably not run.

Moving back to your local machine, you are now able to add this repository as a remote:

git remote add a_descriptive_server_name yourusername@

Now, of course, be sure to change all the obvious stuff above into your own equivalents. Be super sure not to miss the colon after the IP address and before your remote dir path.

At this point you can now complete your first git push - but it won't really do very much:

git push a_descriptive_server_name master

Or any other branch you have.

To make this work as you might expect, we need to add a little something to the post-receive script.

In our case, this would look something like this:

cd /home/myserverusername/git/example.git/hooks
vi post-receive

At this point, I usually do a little copy / paste exercise using this file as a starting point - but this is the actual end result I use in the video:


export GIT_WORK_TREE=/var/www/html/yoursite.tld
export GIT_DIR=/var/git/yoursite.git

git --work-tree=$GIT_WORK_TREE --git-dir=$GIT_DIR checkout -f

** Important ** as mentioned in the video, that script on GitHub should be used for development only, or at the very most, a starting point for your own changes. Don't wipe your vendor directory and rebuild using composer install in production!!!!! (no amount of exclamation points do this point the required level of caution it deserves).

Change the GIT_WORK_TREE and GIT_DIR paths to match your server. In this case, these would be changed to:


export GIT_WORK_TREE=/home/myserverusername/deploy/
export GIT_DIR=/home/myserverusername/git/example.git

git --work-tree=$GIT_WORK_TREE --git-dir=$GIT_DIR checkout -f

All that is left to do now is a:

git push a_descriptive_server_name master

And you should see your local files appear in the right directory on your server. If you don't, you may need to make a quick change / commit / push.

Further Reading

Share This Episode

If you have found this video helpful, please consider sharing. I really appreciate it.

Episodes in this series

# Title Duration
1 Squashing Commits Video Tutorial 05:51
2 Git Push Deploy 08:35
3 Patching with Git (git add -p) 02:53
4 Selectively Removing Parts of a Commit (git reset -p) 05:19
5 Making the most out of Git Log 06:20
6 Git Ignore Woes 04:51