57

I'm having trouble getting post-recieve and post-commit hooks to work correctly with msysgit (Windows 7 Pro/32 and Ultimate/64). For post-commit hook I get the above error if I commit from either git-bash or the console, but it works fine if I commit through git-gui. For a post-recieve hook, all three give the same error.

I'm thinking this is some sort of permission or path error, but don't really have any clue where to start here.

10 Answers 10

115

Add the SHEBANG to the first line of hook, like so:

#!/bin/sh
echo "executing post-commit"
exit 0

This had me stumped for a while as well and I saw that adding the shebang fixed it. In SVN world, while in *nix we have a "pre-commit" script and in Windows we had "pre-commit.bat" and SVN automatically picked up the bat file in Windows. Git doesn't seem to pick up a pre-commit.bat ( or any hook ) and adding the shebang to the hook file worked.

Sign up to request clarification or add additional context in comments.

4 Comments

And by that he means the very first line. I accidentally had an empty new-line at the top and was getting the same error.
Just FYI - the hook file must also be ASCII encoded, I had a tool generating a unicode format hook file, which was getting me the same error since I had some formatting bytes before the bang.
Also FYI: On my system, I had a hook with a perl shebang. But the perl.exe was not in the path. After adding it to the env variable, the error went away.
@mamidon Thanks, in my case it wasn't working because of encoding (UTF-8 with BOM, instead of plain UTF-8)
11

I'm using SourceTree and git LFS and had a similar issue: cannot spawn .git/hooks/pre-push.

The fix was to delete the pre-push file (opening it revealed it was badly corrupted) and restart SourceTree at which point it regenerates the pre-push file and everything is back to normal.

Comments

6

If you have the SHEBANG and it still fails, make sure you have <path_to_git>\bin set in your path environment variable.

You'll probably also have <path_to_git>\cmd if you installed it to work from the command-line.

Comments

4

Got this in a repo using LFS, got rid of it with git lfs update --force

Comments

3

This is an old question, but I've been fighting with this exact problem and this SO question popped up, so I thought it worth the effort to record what worked for me.

In short: I needed to run Apache as a regular user instead of Local System. This was on a legacy test VM I was playing with, so it was only running Windows XP, but it appears that at least on that platform (and possibly others), msysgit just doesn't work properly when running under the Local System account (presumably the root filesystem isn't mapped properly). As a result, no shebang line will work as git-http-backend simply can't execute any msysgit binaries (even with absolute Windows paths).

Switching Apache to run as a regular user account fixed this problem completely. Obviously you need to ensure that the user Apache is running as has permissions to read/write the git repositories, but beyond that, just make sure your shebang line is #!/bin/sh and everything should be copacetic.

Lastly, yeah, this is a big hammer. Ideally you'd be able to use something like suexec on Windows, but a quick googling doesn't indicate an obvious path forward, there. Of course, if anyone has any ideas, I'd be interested.

For now, this works for me, but it doesn't seem ideal.

2 Comments

I believe the problem indeed is to have <path_to_git>\bin in your path. I used a regular user account, and got the same error. Once I have the bin in path, I can run as regular user account or Local System without any problem. In you case, you might have set the path in your regular user account only.
I'm curious as to where Apache fits into all of this. It isn't part of my workflow, but other than that all the symptoms match. Git's there in my global environment PATH (C:\Program Files (x86)\Git\bin and ..\cmd). Am I misunderstanding something?
2

If someone, like me run into a similar problem with accessing git repositories through apache, you should set the PATH in Apache config, like:

SetEnv PATH "c:/Program Files (x86)/Git/bin;"

1 Comment

Thanks @Xmister. We added this to httpd.conf and this appeared to address the issue.
2

Using tortoisegit and LFS, for me just had to remove the files inside of the .git/hooks folder.

Comments

1

If you are using Android studio, you can remove this error by un-check the checkbox "Run Git hooks":

Un-check the checkbox "Run Git hooks", this will solve your problem

1 Comment

Sure, you get rid of the error, but you also don't do whatever the pre-commit script is supposed to do, which can be useful (linting/formatting files, etc.)
0

For me, removing a comment line on front of the shebang line fixed the error. Oddly, the script ran fine from the shell, just errored out when run as a hook.

Comments

0

In my case, I had the error (error: cannot spawn .git/hooks/pre-commit: No such file or directory) because my pre-commit script file was encoded in UTF-8 with BOM.

Changing the encoding to plain UTF-8 fixed it!

Hopefully that will save a headache to some people ^^

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.