Git Post-Commit Hook

2.1k views Asked by At

Git newbie here. I want a post commit hook that when run will copy the latest code from the repository to a different server in a new folder. The folder name should be what I wrote in the git commit comment.

Is this possible?

1

There are 1 answers

2
Vitor On

Since you don't look so certain as to which hook you need, according to the coments, I'm posting an answer based on post-receive hook. This is a server-side hook that will ran after everything is received in the server, after a push. A post-commit hook would be client-side, and would run after every commit (for more info about git hooks, see https://git-scm.com/book/it/v2/Customizing-Git-Git-Hooks).

Assuming a bash script, you can use:

MESSAGE=`git log -1 HEAD --pretty=format:%s`

to retrieve the last commit message, and work from there to create your script. It's also possible to retrieve every commit message included in that push, by using:

while read oldrev newrev ref
do
    MESSAGE=`git log -1 $newrev --pretty=format:%s`
    doSomethingElse...
done

As for copying it to somewhere else, you can use anything you'd use in other scripts, like scp, rsync... Just clone/update a local copy in a temp dir and sync it to your remote location.