FTP Is So 90's. Deploy With Git.

First, create a directory on your server and initialize an empty git repository. I like to serve my websites from ~/www/, so that’s what I’ll do in this example.

mkdir ~/www/example.com && cd ~/www/example.com
git init

Next, let’s set up your server’s git repo to nicely handle deployment via git push.

git config core.worktree ~/www/example.com
git config receive.denycurrentbranch ignore

Finally, we’ll set up a post-receive hook for git to check out the master branch so your web server can serve files from that branch. (Remember, ^D is Control+D, or whatever your shell’s EOT character is.

cat > .git/hooks/post-receive
#!/bin/sh
git checkout -f
^D
chmod +x .git/hooks/post-receive

Keep in mind that you can add whatever you like to the post-receive hook if you have a build process. For example, one of my sinatra projects uses the following post-receive hook:

#!/bin/sh
git checkout -f
bundle install
touch ~/www/example.com/tmp/restart.txt

Back on your local machine, let’s get your git repo ready for deployment.

cd ~/www-dev/example.com
git remote add origin \
ssh://user@example.com/home/user/www/example.com

For the first push to your server, run the following command.

git push origin master

Now, whenever you want to deploy changes you’ve made locally, simply run the following command!

git push

comments powered by Disqus