Simple git post-commit hook to copy committed files to a certain folder
Asked Answered
S

1

25

I would like to automatically copy the committed files to a certain folder so they can be viewed in a browser, but I would like to do this without having to create a bare repository that mirrors the main repository (as shown here) and I would like this to happen on commit.

Is there any simple way to create a hook that reads which files have been committed and copies them/updates them on a live webserver?

For example: I have a folder named /example.com and a git repository. I want that when I commit index.html in the repository, the corresponding index.html file from /example.com to be updated with the contents of the committed file

Spreader answered 3/10, 2011 at 9:39 Comment(0)
S
29

A good way of doing this is to create a post-commit that runs git checkout -f with the work tree set to the directory that is exposed by your web server and the git directory set to the .git directory in your development repository. For example, you could create a .git/hooks/post-commit file that did:

#!/bin/sh
unset GIT_INDEX_FILE
export GIT_WORK_TREE=/example.com/
export GIT_DIR=/home/whoever/development/web-project/.git/
git checkout -f

Be careful with this, however - the -f means that git may remove or overwrite files to make /example.com/ match the tree in your most recent commit.

(Remember to make the .git/hooks/post-commit file executable as well.)

Syst answered 3/10, 2011 at 15:24 Comment(3)
On windows, is the script expecting full paths and what is the syntax? Thanks!Configuration
@Configuration I'm using msysgit, and it works using the same paths you would use in a git bash window.Hass
Does this rewrite all the files or just the new ones? I have a very large repo and don't want to copy them all up every time.Piscatory

© 2022 - 2024 — McMap. All rights reserved.