How to run a powershell script as a Git hook

How to run a powershell script as a Git hook

  • coding
  • 2024-03-09
  • 3 minutes to read

Introduction

Git hooks are scripts that run automatically every time a particular event occurs in a Git repository. They let you customise Git’s internal behaviour and trigger customisable actions at key points in the development lifecycle. In this post, I will look at how to run a powershell script as a Git hook. In this guide I am going to create a powershell script that raises an error and prevents a commit from being made. This isn’t going to be a very popular hook, because it will prevent any commits from being made, but it will get you as far as being able to run a powershell script as a Git hook.

Create a powershell script

I am going to create a powershell script that raises an error and returns an error code. This script will be called pre-commit.ps1 and I am going to put it in a scripts folder in my repository.

## scripts/pre-commit.ps1
Write-Error "You shall not pass!"
exit 1

This script will raise an error and return the message “You shall not pass!” and then return the code 1. This will be passed back to the pre-commit hook and will prevent the commit from being made.

Create a pre-commit hook

In your repository there is a .git directory that is hidden by default. Within there you will find a hooks directory that contains a number of sample hooks. To use one of the sample hooks you simply remove the extension. Starting with the sample pre-commit hook, I will remove the commands and replace them with a call to the powershell script. The script will look like this:

#!/bin/sh
echo "Starting pre-commit hook"
exec pwsh -File scripts/pre-commit.ps1
echo "Completed pre-commit hook"

I will then save the file as .git/hooks/pre-commit, by doing so git will automatically run this script every time a commit is made (and crucially before the commit is done).

Make the script executable

If you duplicate the sample file, the file will already be executable however, if you started from an empty file you will need to make the file executable. You can do this by running the following command:

chmod +x .git/hooks/pre-commit

Test the hook

And now I can test the hook by making a commit. To do so, I will make a change to a file then run git commit and see what happens:

the output from git says 'Start pre-commit-hook' followed by 'Write-Error: You shall not pass!'

Perfect!

Conclusion

So that’s how you can run a powershell script as a Git hook. This is a very simple example and it isn’t going to help very much because it will prevent you committing any code! From here you can extend the powershell script so that it performs whatever checks you wish, and then only use the exit 1 command when you want to prevent the commit to occur. You can also use other hooks to run the script at different times in the development lifecycle.

References

#mtfbwy



Recent Posts

How to Search for a Lost File in the Git Log

How to Search for a Lost File in the Git Log

  • 2024-04-27
  • 4 minutes to read

I have lost a file in my Git repository. How can I find it by searching the git log?

Read More
No Such Shell Function 'Zle Line Init' in Zsh

No Such Shell Function 'Zle Line Init' in Zsh

  • 2024-04-25
  • 3 minutes to read

Troubleshooting the error message "no such shell function 'zle line init'" in zsh when using OhMyPosh.

Read More
Getting Started With Python in Vscode

Getting Started With Python in Vscode

  • 2024-04-05
  • 2 minutes to read

This post will help you get started with Python in Vscode and identify some of the useful extensions to install.

Read More