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