强制git运行post-receive hook,即使所有内容都是“最新的” [英] Force git to run post-receive hook, even if everything is "up-to-date"

查看:246
本文介绍了强制git运行post-receive hook,即使所有内容都是“最新的”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何强制 git 在服务器上运行 post-receive 钩子,即使我没有有一个新的提交推送?



背景



我使用git自动将网站部署到服务器。我在服务器的一个受保护区域有一个裸回购和一个 post-receive 挂钩,它检查内容并系统地将某些文件复制到 public_html 文件夹。 (受本教程的启发)



我厌倦了在服务器上手动修改 post-receive 钩子,所以现在我的 post-receive 钩子实际上是从repo复制一个新版本:

 #!/ bin / sh 

rm -rf〜/../ protected / *
GIT_WORK_TREE =〜/../受保护的git checkout -f

#使用post-receive文件中的任何更新重写此文件
cp〜/../ protected / post-receive hooks / post-receive

#删除public_html
#复制内容public_html

这个问题当然是,新的 post-receive 挂钩永远不会运行。一个看似简单的解决方案只是再推一次,但现在一切都已经是最新的了。这很烦人,因为它需要我每次更新 post-receive 钩子时伪造一个新的提交。有没有办法在不伪造commit或 ssh ing的情况下调用 post-receive 钩子?

我试过的东西



  git push 
git push -f


解决方案

使用'--allow-empty'



在初次推送替换脚本后,您可以这样做:

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

- allow-empty 标志覆盖git的默认行为,以防止在没有更改时进行提交。 strong>使用别名,让您的生活更轻松



将以下内容添加到〜/ .gitconfig

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

现在只要执行 git pushpr

  git pushpr 

这将推动对master的任何更改,在您的情况下会触发您的post接收替换脚本,然后再次推送(使用 - allow-empty 标志),然后它将执行更新的 post-receive 脚本。


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

解决方案

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.

这篇关于强制git运行post-receive hook,即使所有内容都是“最新的”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆