Feb 26, 2012
tom

How do I deploy a public git repo to a hosted solution via command line?

Question

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?

Asked by Code Junkie

Answer

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:

#!/bin/bash
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.

Answered by David Zaslavsky

Related posts:

  1. removed large files from git repo; repo still humongous
  2. Trying to create a git repo that does an automatic checkout everytime someone updates origin
  3. What are the dangerous operations that need to be prohibited on a hosted git repository?
  4. Is the port number the same when connecting to git via the git+ssh protocol?
  5. Easy way to deploy PHP sites from git

Leave a comment