I have a few sites which are all hosted on the same web hosting service under shared hosting. My web host supports Git and I have SSH access to it, and I also have Git setup on my laptop as well.

I want to make it so that when I do a "git push origin master", it will automatically update the files on my web server, and also save a backup of the previous commit's files so I can easily rollback if I want to. Is this possible?

  • Curious- who is your hosting provider?
    – user
    Sep 8, 2013 at 19:33
  • And why would you want a "backup of the previous commit's files"? You can just push the previous commit, if you want to roll back (assuming you always know what you pushed last - but you should know that anyway).
    – sleske
    Mar 31, 2015 at 7:57

2 Answers 2


This is summarized from Using Git to manage a web site

The key to the process is the server side hook 'post-receive' (more on git hooks at Customizing Git - Git Hooks and the githooks man page). This hook runs after the server has received all of the data.

Once the server receives the data, it runs git checkout -f The -f option will force a checkout to the head even if there are local differences.

GIT_WORK_TREE=/var/www/www.example.org git checkout -f

Put that in the hooks/ directory as post-receive and executable. Of course, the path changes to where you have your webserver's files (the use of GIT_WORK_TREE sets the environment variable so that you don't need to juggle dot files and git settings on the server).

For rolling back, one should tag each release (this can be done as part of the post-commit hook too). By tagging the release one can easily identify the spot to rollback to, though that likely involves logging into the server and checking out that tag.

  • It should be noted that this means you are using git as a deployment tool, which is something it is not really intended for. It can work, but there are numerous limitations (need to download whole repo, no way to invoke scripts or manage file permissions, no way to unpack stuff...). It's probably better to use a proper deployment tool.
    – sleske
    Mar 31, 2015 at 7:56
  • @sleske You can do all that in the post-receive hook, which is really just a script where you can put in whatever you want.
    – Mario
    Mar 31, 2015 at 8:02
  • @Mario: Yes, you can - which means you are effectively implementing your own deployment solution as a post-receive hook. There are still benefits to using an existing solution, but sometimes roll-your-own may be best...
    – sleske
    Mar 31, 2015 at 8:30
  • You may even checkout into a intermediate folder and have links from the web folder to the intermediate folder to avoid having .git files in your web folder.
    – Bent
    Jan 18, 2018 at 20:01

The simplest way to update the working tree of the repository you are pushing to is setting git config receive.denyCurrentBranch updateInstead on the receiver side. See https://git-scm.com/docs/git-config/#git-config-receivedenyCurrentBranch

Ryan's answer with post commit hooks is better in that it allows checking out to some different location (you likely do not want to have the .git in your web folder). But at this level, it might be a good idea to use some existing deployment tool, like sleske said in the comments.

Not the answer you're looking for? Browse other questions tagged or ask your own question.