Take the 2-minute tour ×

I am using a personal project as practice for some of the more advanced things we're doing at work. One of the things I want to learn to do is to publish a project managed in a public git repo directly to a sub-domain on my server. Ideally, I'd like to be able to just do something like: typing git deploy in terminal to push the project to the live hosting. Is something like this possible? How can I do it?

share|improve this question
Are you asking about how to push to a Github repository? If so, they give you clear directions which you should follow, and if not, it'd be best to remove that tag from the question. –  David Z Feb 26 '12 at 3:35
@Chad Should I delete this question and ask it on Server Fault instead? –  Code Junkie Feb 26 '12 at 4:01
@DavidZaslavsky I'm using GitHub to manage my public project, but I'm asking about pushing a usable version to a subdomain on a hosting solution. –  Code Junkie Feb 26 '12 at 4:01
OK, so not really pushing (which has a specific meaning in git) so much as deploying. I think I can write up an answer for you. Also, don't delete and re-ask; the question will be migrated for you if it belongs elsewhere. –  David Z Feb 26 '12 at 4:08
Thanks! If it's important, I'm using Ubuntu. –  Code Junkie Feb 26 '12 at 4:15
add comment

migrated from stackoverflow.com Feb 27 '12 at 2:50

This question came from our site for professional and enthusiast programmers.

1 Answer

up vote 3 down vote accepted

It sounds like you're asking not how to push the project to a remote repository, but how to actually check it out into a directory on a filesystem from which files are served by your web server.

You'll need to have a remote repository on the same machine as the web server (or more precisely, a repository on a machine which can write to the filesystem that the web server has access to). You can set up a post-receive hook in that repository, which is a script that will get run every time something is pushed to the repository. The post-receive hook should run a git checkout command which will check out the newly pushed files on to the actual filesystem, and from there they can be served by your web server. The script will look something like this:

git --work-tree=/path/to/document/root checkout -f
# reload web server here, if you need to

I use a similar setup for my own website, and I've written a blog post describing it. You might want to look at it and see if the information there helps you. I use a staging server in addition to the live server, but if you don't, my post includes a couple of links (1, 2) that describe alternate, simpler setups.

In your particular case, since you have a remote repository on Github, you may want to push changes to both the Github repository and the one on your web server at the same time, so that the two are always in sync. To do so, you can put multiple push URLs into the same remote, using the git remote add-url command, so that when you run git push, it will send your changes to both Github and the server remote. (See git help remote) Alternatively, you could consider using your Github repository as a staging repository, as described in my blog post, or you could just manually ssh into your web server and pull changes any time you need to update your website. These latter two solutions would mean your Github repository wouldn't necessarily be in sync with your web server; that would be desirable if you sometimes push changes to Github which you don't want reflected on your live website.

share|improve this answer
Your second two suggestions sound like the best ones to me, since part of what I want to teach myself is how to work locally on client sites, and then push finished versions to the server. Ideally, I would like to create subdomains like: beta.myclientsite.com as a staging area, and then push approved versions to the main domain. –  Code Junkie Feb 26 '12 at 4:30
So basically, I set up a get repo on beta.myclientsite.com, and a repo on myclientsite.com, push to beta, then a post-receive hook copies the files to the root directory, I test there, and then do the same thing on live? –  Code Junkie Feb 26 '12 at 5:28
Yeah, pretty much. (If both the beta and live domains are hosted on the same server, then you can actually use one git repository and just have two branches which get checked out to different locations.) –  David Z Feb 26 '12 at 6:08
add comment

Your Answer


