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.
Here is a script:
#!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.
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.
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"Can't" is a strong word. You can, but only via Rube Goldberg machinery that I would recommend avoiding.