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
Table of Contents

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

Hugo Live Reload Keeps Reloading

Hugo Live Reload Keeps Reloading

  • 2024-05-18
  • 3 minutes to read

I have run into an issue from time to time where Hugo's live reload feature keeps reloading the page. This is how I fixed it.

Read More
First Steps with Mermaid in Vscode

First Steps with Mermaid in Vscode

  • 2024-05-17
  • 7 minutes to read

My blog theme supports mermaid, a visual diagram syntax, so I wanted to try it out in vscode.

Read More
T-SQL Tuesday #174 My favourite job interview question

T-SQL Tuesday #174 My favourite job interview question

  • 2024-05-14
  • 4 minutes to read

This is my 12th contribution to TSQL Tuesday, the data community blog party. Have a read, and why not join in next time!

Read More