Skip to main content
Photo by Roman Synkevych via Unsplash

Git’s missing status Hook

Despite its many advantages and nifty features of Git, there’s one feature it lacks: a status hook.

In Git, hooks are scripts that I can execute automatically at specific points during the version control process. These hooks perform custom actions, such as running tests, enforcing coding standards, or sending notifications, at various stages like pre-commit, post-commit, and post-merge. However, Git does not provide a built-in hook specifically for the status command.

So, what would a status hook be good for? Imagine I’m working on a project that relies on external packages or modules that are not part of my repository. For instance, I might be using Go modules from a separate repository. A status hook could check if these external dependencies are up to date and notify me if they’re not. It can even prevent me from proceeding with further actions until these dependencies are in sync.

It’s quite easy to create my own status hook though. The following script, placed into my .bashrc, serves as a wrapper for the git command, executing a status hook when running git status:

1git()
2{
3    FILE=.git/hooks/status
4    if [[ $# -ge 1 && "$1" == "status" && -x "$FILE" ]]; then
5        bash "$FILE"
6    fi
7    command git "$@"
8}

Let’s break down what this script does:

  • It sets the FILE variable to the path of my custom status hook, which is .git/hooks/status. I decided to keep it in the place where one would expect hook scripts. To make it easy and transportable, it contains only a call to the actual hook script, which is located elsewhere and part of the repository.
  • The script checks if I’m running the git command with the argument status.
  • If the conditions are met and the status hook script is executable (-x), it executes the custom hook script.
  • Finally, it ensures that the original git command is always executed, passing all the provided arguments to it.

I could use a npm script instead of the bash script, but, well, here we are.

Back to top
Back Forward