The power of git to run post-receive hook, even if everything is "updated",

How to get git to start post-receive binding on the server, even if I don't have a new commit to push?

Background

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

I'm tired of manually changing the post-receive hook on the server, so my post-receive hook now actually copies the 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 trick never starts. It would seem that a simple solution would be to simply click again, but now everything is already updated. 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 call a post-receive hook without commit fake or ssh ing?

What i tried

 git push git push -f 
+52
git
Dec 03 '12 at 4:26
source share
6 answers

Use '-allow-empty'

After the first click of the script button, you can do this:

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

The --allow-empty flag overrides the default git behavior, which prevents committing when there are no changes.

Use a nickname and simplify your life

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 result in any changes to master that in your case will cause a replacement for your email script message, then it will be pressed again (using the --allow-empty flag), which will then execute your updated post-receive script.

+41
Feb 24 '15 at 18:12
source share

I know this is likely to be considered β€œdangerous,” but I like living on the edge.

I just delete the remote branch and push it again. Make sure your local branch is updated first to limit the chance of material loss.

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

 $ git push origin :testing $ git push origin testing 

Do not take this as an answer, though. This is most likely just FYI.

+18
Aug 07 '14 at
source share

I am afraid that you should ssh to the server and run the hook script manually. git push does not force the server to execute pre and post and post hooks if nothing is added (i.e. when git prints everything updated).

The rest of the answer is about tracking the tracking version after receiving it, so you can change it without sshing to the server.

Add a shell script called 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 hook hooks/post-receive on the server:

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

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

Configure your changes from the local repository to the server and see how your do-post-receive code runs:

 $ git push origin master ... remote: Hello, World! ... 
+12
Apr 13 '13 at 7:47
source share

I tried blank commits and delete the download branch .

But what about the direct ssh command:

 ssh your_host /path/to/your_repo/hooks/post-receive 
+3
Sep 08 '16 at 0:10
source share

I like Jamie Karl's suggestion, but it doesn't work, and I got an error:

remote: error: by default, deleting the current branch is rejected because the following error: 'git clone' will not cause the file to be extracted, which will lead to confusion.

In my case, I am testing post-receive hooks on my localhost against an open repository. Warning / super important, run this command only on the remote server!

 git update-ref -d refs/heads/develop 

It will remove the link for the development branch (you may also need to delete any of the files that you deployed for this branch), then you can go and do git push deploy_localtest develop or any push command you want for this branch.

+1
Dec 17 '15 at 4:46
source share

In my case, I go into the remote and run:

$ sh project.git/hooks/post-receive

works great!

0
Dec 05 '17 at 17:24
source share



All Articles