msysgit error with hooks: "git error: cannot spawn .git/hooks/post-commit: No such file or directory"

WindowsGitMsysgit

Windows Problem Overview


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.

Windows Solutions


Solution 1 - Windows

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.

Solution 2 - Windows

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.

Solution 3 - Windows

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.

Solution 4 - Windows

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.

Solution 5 - Windows

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

Solution 6 - Windows

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;"

Solution 7 - Windows

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

Solution 8 - Windows

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.

Solution 9 - Windows

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

Un-check the checkbox

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMattView Question on Stackoverflow
Solution 1 - WindowsmanojldsView Answer on Stackoverflow
Solution 2 - WindowsJarrod SmithView Answer on Stackoverflow
Solution 3 - WindowsNick RobsonView Answer on Stackoverflow
Solution 4 - Windowsuser1630539View Answer on Stackoverflow
Solution 5 - WindowsHemaolleView Answer on Stackoverflow
Solution 6 - WindowsXmisterView Answer on Stackoverflow
Solution 7 - WindowsJustin HirschView Answer on Stackoverflow
Solution 8 - WindowsT CormanView Answer on Stackoverflow
Solution 9 - WindowsDeepakView Answer on Stackoverflow