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

后端 未结 9 1731
再見小時候
再見小時候 2020-11-30 19:27

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 aut

相关标签:
9条回答
  • 2020-11-30 19:45

    I'm afraid you have to ssh to the server and run the hook script manually. git push doesn't make the server run the pre-push, pre-receive and post-receive hooks if there was nothing added (i.e. when git prints Everything up-to-date).

    The rest of the answer is about version-tracking the post-receive hook, so you can modify it without sshing to the server.

    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!
    ...
    
    0 讨论(0)
  • 2020-11-30 19:46

    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.

    0 讨论(0)
  • 2020-11-30 19:49

    In my case i login into remote and run:

    $ sh project.git/hooks/post-receive

    works fine!

    0 讨论(0)
提交回复
热议问题