使预推钩将推送内容的一部分更改到git服务器 [英] Make pre-push hook changes part of pushed content to git server

查看:45
本文介绍了使预推钩将推送内容的一部分更改到git服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在git pre-push hook上执行的脚本.里面有PowerShell脚本,用于修改存储库中的项目文件.该文件将在本地进行适当的修改,但更改不会在git服务器上结束.

I have a script which executes on git pre-push hook. Inside there is PowerShell script which modifies project file in repository. This file will be properly modified localy but that change won't end up on the git server.

这是一个脚本:

#!C:/Program\ Files/Git/bin/sh.exe
branch=`git rev-parse --abbrev-ref HEAD`

if [ $branch = "development" ]; 
then
    exec powershell.exe -NoProfile -ExecutionPolicy Bypass -File update_version.ps1 -mode dev
pwd
fi

exit 

推荐答案

As phd said in a comment, you can't.1 The reason is that git push pushes commits, not files.

确实提交了包含个文件-每个提交都是该提交中 all 个文件的完整快照-因此git push最终将发送文件以及提交;但是此问题的关键是git push在您自己的Git存储库中找到了一组提交,而您要推送到的其他Git却没有 .您的Git打包这些提交(作者和日志消息,先前的提交哈希,源树的快照等)并将其发送.预推挂钩旨在测试是否发送这些提交.

It's true that commits contain files—each commit is a complete snapshot of all the files that are in that commit—so git push winds up sending files along with the commits; but the key to this problem is that git push finds some set of commits, in your own Git repository, that the other Git to which you are pushing does not have. Your Git packages up those commits—the author and log message, previous commit hash, snapshot of source tree, and so on—and sends them. A pre-push hook is intended to test whether or not to send these commits.

您应该(请参见脚注1)将您的推送前脚本编写为 check ,以确保适当的版本信息存在;否则,请中止推送.然后,而不是运行git push,运行my-script-that-updates-and-then-runs-git-push.该脚本将进行适当的更新和提交,然后运行git push,尽管进行了检查,该脚本仍然成功,然后执行其他任何适当的操作.

You should (see footnote 1) write your pre-push script to check to make sure that the appropriate version information is in place, and if not, abort the push. Then, instead of running git push, run my-script-that-updates-and-then-runs-git-push. That script will update and commit as appropriate, then run a git push that succeeds despite the check, then do whatever else is appropriate if anything.

1 不能"是一个很强的词.您可以,但只能通过我推荐的 Rube Goldberg机械避免.

1"Can't" is a strong word. You can, but only via Rube Goldberg machinery that I would recommend avoiding.

这篇关于使预推钩将推送内容的一部分更改到git服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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