I have several sites that I manage in one form or another, and a couple years ago I made a huge move from sending files over FTP to maintaining a remote repository on the server. Originally Mercurial - now Git. Still, same concept:
- Push to a central repository like Bitbucket or GitHub.
- Create a local clone on your production server in your /var/www directory or whatever
- When you make a new change, commit it locally…
- … push to GitHub …
- … ssh into your production server …
- Do a
git pullfrom GitHub
This is better than FTP, but still isn’t ideal. First, it puts either an .hg or .git folder in the root of your production code. But also, it comes with the added step of having to SSH into your server.
With all the wonderful things Heroku can do with their
git push heroku master, I figured I’d go looking for a better solution. I found it here:
Here’s the core of the post:
- Make sure you can SSH into your server with your private key. Plenty of articles on how to do that. So in a terminal you should be able to type:
ssh email@example.com it’ll log you in without asking for a password.
mkdir website.git && cd website.git
git init --bare
- Create a new post-receive hook in git by creating a file in hooks/post-receive with this:
- Make sure it’s executable:
chmod +x hooks/post-receive
- Then on your local repository:
git remote add web ssh://firstname.lastname@example.org/website.git
- And the key part:
git push production +master:refs/heads/master
Now, from your local git repository, you can do this:
git push production
And your changes automatically move to the live server!