Jay Taylor's notes

back to listing index

Force git to run post-recieve hook, even if everything is "up-to-date" - Stack Overflow

[web search]
Original source (stackoverflow.com)
Tags: git deployment post-receive hooks up-to-date stackoverflow.com
Clipped on: 2013-08-22

How do I force git to run a post-recieve hook on a server even if I don't have a new commit to push?

Background

I use git to automatically deploy a website to a server. I have a bare repo in a protected area of the server and a post-receive hook that checks out the contents and systematically copies over certain files into a public_html folder. (Inspired by this tutorial)

I got tired of modifying the post-recieve hook manually on the server, so my post-receive hook now actually copies over a new version of itself from the repo:

#!/bin/sh

rm -rf ~/../protected/*
GIT_WORK_TREE=~/../protected git checkout -f

# Rewrite over this file with any updates from the post-receive file
cp ~/../protected/post-receive hooks/post-receive

# Delete public_html
# Copy stuff public_html

The problem, of course, is that the new post-receive hook never gets run. A seemingly simple solution would be merely to push again, but now everything is already up to date. This is annoying, because it requires me to fake a new commit every time I update the post-receive hook. Is there a way to invoke the post-receive hook without faking a commit or sshing in?

What I tried

git push
git push -f
asked Dec 3 '12 at 4:26
Image (Asset 1/3) alt= 3,18542438
add comment

2 Answers

Add a shell script named `do-post-receive' to the local repository:

$ ls -ld .git
$ echo 'echo "Hello, World!"' >do-post-receive
$ git add do-post-receive
$ git commit do-post-receive -m 'added do-post-receive'

Replace your hooks/post-receive hook on the server with:

#! /bin/sh
while read OLDID NEWID BRANCH; do
  test "$BRANCH" = refs/heads/master && eval "$(git show master:do-post-receive)"
done

(Make sure to chmod 755 hooks/post-receive on the server.)

Push your changes from the local repository to the server, and watch your do-post-receive code run:

$ git push origin master
...
remote: Hello, World!
...
answered Apr 13 at 7:47
Image (Asset 2/3) alt= 9,61533068
  upvote
 flag
Very clever! Unfortunately I may not be able to try this out for awhile to give the accept... But be patient with me. – AndyL Apr 13 at 11:29
  upvote
 flag
Sounds promising, more than my answer anyway. +1 – VonC Apr 13 at 15:08
add comment

In short, no, it doesn't seem to be possible to make that particular hook run when no commits are pushed.

Which probably means you are not using the right mechanism to update said hook.
A server hook is likely to not be called if the git detects "everything is up to date" on the client side.

answered Dec 3 '12 at 7:00
Image (Asset 3/3) alt= 323k72697822
add comment

Your Answer

 
community wiki

Not the answer you're looking for? Browse other questions tagged or ask your own question.