Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force git to run post-receive hook, even if everything is "up-to-date"

Tags:

git

How do I force git to run a post-receive 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-receive 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 
like image 627
AndyL Avatar asked Dec 03 '12 04:12

AndyL


People also ask

How do you enforce a pre-commit hook?

If you want enforcement, use an update hook in the central repo. If the hook is doing per-commit verification, you can still provide a pre-commit hook; developers will likely adopt it voluntarily, so that they can find out right away when they've done something wrong, rather than waiting until they try to push.

How do I bypass git pre-commit hook?

Use the --no-verify option to skip git commit hooks, e.g. git commit -m "commit message" --no-verify . When the --no-verify option is used, the pre-commit and commit-msg hooks are bypassed.

How do I force a git commit?

The --force option must be used to push an amended commit. The above example assumes it is being executed on an existing repository with a commit history. git commit --amend is used to update the previous commit. The amended commit is then force pushed using the --force option.


2 Answers

Use '--allow-empty'

After the initial push replacing the script, you can do this :

git commit --allow-empty -m 'push to execute post-receive' 

The --allow-empty flag overrides git's default behavior of preventing you from making a commit when there are no changes.

Use an alias and make your life even easier

Add the following to ~/.gitconfig

[alias]     pushpr = "!f() { git push origin master;git commit --allow-empty -m 'push to execute post-receive';git push origin master; }; f" 

Now Just do git pushpr

git pushpr 

This will push any changes to master, which in your case will trigger your post receive replacement script, then it will push again (using the --allow-empty flag) which will then execute your updated post-receive script.

like image 167
AndrewD Avatar answered Sep 19 '22 21:09

AndrewD


I know this probably going to be considered "dangerous" but I like to live on the edge.

I just delete the remote branch and then push it again. Make sure your local branch is up-to-date first to limit the chance of losing stuff.

So if I want to trigger post-receive, in my case to get the testing branch to provision, all I do is:

$ git push origin :testing $ git push origin testing 

Don't accept this as the answer though. It's more of a just FYI thing.

like image 43
Jamie Carl Avatar answered Sep 20 '22 21:09

Jamie Carl