In Posh, git hooks live in the same place and still have to be executable on your platform. That doesn’t change. But, the scripts themselves can be different. You have two options.
Option 1: Don’t Use PowerShell
Your existing hooks written in bash or zsh or whatever Linux-ey shell you were using will still work. That’s great if you already have a bunch and you don’t want to port them all.
If you’re writing anything new, though, use PowerShell. When I get into a mess on my Posh Apple, it’s usually because I mixed PowerShell with the legacy shell. You’re better off using just one.
Option 2: Update the Shebang
The shebang (
#!) is the first line of executable scripts on Unix-like systems. It sets the program that’s used to run the script. We just need to write one in our hook script that points at
pwsh (the PowerShell executable):
#!/usr/local/microsoft/powershell/7/pwsh Write-Verbose -Verbose "We're about to commit!"
If you don’t have the path to your
pwsh, you can find it with
After that, our hook works like normal:
git commit --allow-empty -m "Example commit." VERBOSE: We're about to commit! [master a905079] Example commit.
If you don’t set the shebang at all (leaving nothing but the
Write-Verbose command in our example), your hook will run but OS X won’t treat it like PowerShell. You get “not found” errors:
git commit --allow-empty -m "Example commit." .git/hooks/pre-commit: line 2: Write-Verbose: command not found [master 1b2ebac] Example commit.
That’s actually good. If you have old hook scripts without shebang lines, they won’t break. Just make sure any new Posh scripts do have a shebang and everything should work.
Enjoy the Posh life!
Need more than just this article? We’re available to consult.
You might also want to check out these related articles: