Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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

like image 342
kioleanu Avatar asked Oct 03 '11 09:10

kioleanu


People also ask

How do you make a commit hook?

Open a terminal window by using option + T in GitKraken Client. Once the terminal windows is open, change directory to . git/hooks . Then use the command chmod +x pre-commit to make the pre-commit file executable.

What is post commit hook in git?

The post-commit hook is called immediately after the commit-msg hook. It can't change the outcome of the git commit operation, so it's used primarily for notification purposes. The script takes no parameters and its exit status does not affect the commit in any way.

How do I add a pre-commit hook to my repository?

If you want to manually run all pre-commit hooks on a repository, run pre-commit run --all-files . To run individual hooks use pre-commit run <hook_id> . The first time pre-commit runs on a file it will automatically download, install, and run the hook. Note that running a hook for the first time may be slow.

Can Git hooks be committed?

It's important to note that Git hooks aren't committed to a Git repository themselves. They're local, untracked files.


1 Answers

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.)

like image 73
Mark Longair Avatar answered Oct 22 '22 14:10

Mark Longair