Checkout Git files to a location outside the repository


Is it possible to “checkout” to a location outside of the repository? What I mean is a clone of a branch without the “.git” directory.

For example: Use git to manage a website. You edit some files, commit and copy the files to a web server via WebDAV. The WebDAV-path would be the location outside the repository, which shouldn't contain the “.git”.

This may not be the problem for which git was build, but is this possible?

Best Answer

Yes, this is certainly possible.

Lets say I have a bare repository named /srv/production.git on the destination system/server.

On the destination system I can use the command cd /srv/production.git; GIT_WORK_TREE=/srv/production-www/ git checkout -f

If I am in a non-bare repository the command is slightly different. Lets say I have a non-bare respository at /srv/testing. To checkout that I would use cd /srv/testing/.git; GIT_WORK_TREE=/srv/production-www/ git checkout -f

In fact on my system I even automate this in a post-commit hook in my production.git repository. So when you push to production.git the latest version is automatically check out to the web root.

# An example hook script that is called after a successful
# commit is made.
# To enable this hook, rename this file to "post-commit".

GIT_WORK_TREE=/srv/production-www/ git checkout -f
# ... misc other commands to verify permissions are set correctly.

If your only access to the remote system is webdav, it certainly should be possible to write a post-commit hook that will checkout to the webdav host, either directly or to a temporary location, which you can then script an upload.

Related Question