GitKraken Desktop Documentation

Git Hooks

Last updated: May 2025

Git hooks are shell scripts that execute after Git events such as commits or pushes.

Overview of Git hooks and demonstration in GitKraken Desktop

Where are Git hooks?

Hooks reside in the hooks subdirectory of the .git folder, created automatically when initializing a repository in GitKraken Desktop. You’ll find it at .git/hooks, typically containing a README.sample file.

Hooks are specific to your local repository and are neither tracked by Git nor copied to new repositories.

Terminal view of the .git/hooks directory
Explorer view of the Git hooks directory

If you’re on OSX or Linux, you must set hook files to be executable. GitKraken Desktop will throw an error (e.g., exit code 126) if the file lacks executable permissions.

Git hook execution error in GitKraken Desktop (exit code 126)

Any script that returns a non-zero exit code is considered a failure.


Define a custom hook path

To use a custom Git hook path:

  1. Go to Preferences Git Hooks.
  2. Click to set or enter the path.

This setting is defined per repository.

Setting a custom Git hooks path in Preferences

What hooks are supported by GitKraken Desktop?

GitKraken Desktop supports a wide array of Git hooks. Each hook is triggered by specific actions like commit, merge, or rebase.

pre-commit

  • Amend
  • Commit
  • Merge Resolve

prepare-commit-msg

  • Amend
  • Commit
  • Cherrypick
  • Merge
  • Revert
  • Squash

commit-msg

  • Amend
  • Commit
  • Merge Resolve

post-commit

  • Amend
  • Cherrypick
  • Commit
  • Merge Resolve
  • Revert

pre-rebase

  • Rebase
  • Squash

post-checkout

  • Checkout
  • Discard Changes (selectively)

post-merge

  • Fast-Forward
  • Merge (Without Conflicts)

post-rewrite

  • Amend
  • Rebase
  • Squash

pre-push

  • Delete Remote Branch
  • Delete Remote Tag
  • Push Branch
  • Push Tag

Git hooks example

Git hooks automate actions when Git events occur in GitKraken Desktop or the command line. Here’s a basic pre-commit hook example to validate the global Git user email.

Tools needed

Step-by-step setup

1. Navigate to hooks directory
Open Visual Studio Code and go to ~/repo/.git/hooks. Create a new file named pre-commit.

Creating a new Git hook file in VS Code

Note 📝 – You may need to make the .git folder visible in VS Code by adjusting files.exclude settings.

2. Make the file executable
Open the GitKraken Terminal and change to the .git/hooks directory. Run:

chmod +x pre-commit
Making the pre-commit file executable using terminal

Note 📝 – If your terminal isn’t set up in GitKraken Desktop, refer to the Start Here Tips.

3. Write your bash script
Use a text editor to define your pre-commit script. Start with the correct shebang:

#!/bin/bash

Then add your validation logic using variables like:

  • globalEmail – value from Git global config
  • workEmail – required email for commits

The script should compare the current global email with the required email and display an error if they do not match. If the values match, the commit proceeds.

Example:

#!/bin/bash
globalEmail=$(git config --global user.email)
workEmail="[email protected]"

if [ "$globalEmail" != "$workEmail" ]; then
  echo "Error: global email does not match required work email ($workEmail)."
  exit 1
fi
exit 0

Save the file. Your pre-commit hook is now ready to enforce proper committer email usage before any commit is made.

Git hook in action

Git hook example triggered during commit

Environment Variables & Git Hooks

On macOS, GUI applications do not inherit shell profile variables. If your Git hooks rely on environment variables set in your shell, use the following command to make them available:

launchctl setenv YOURVAR value

Bypass Git hooks

To skip Git hooks during a commit, use the Commit and skip hooks option.

Note 📝 – This option disables all hooks triggered by the commit action.

Option to commit while skipping hooks in GitKraken Desktop

Global Git Hooks

GitKraken Desktop respects global Git hook paths set in your .gitconfig file. These hooks apply to all cloned repositories.

To configure a global path, add the following to your .gitconfig:

[core]
    hooksPath = /path/to/your/hooks
Have feedback about this article? Did we miss something? Let us know!
On this page