GitKraken Desktop Documentation

Downloads  
Contact support  
Feedback  
GitKraken.com  
GitKraken Status  

Git Hooks

Git hooks are shell scripts that execute after an event such as a commit or push.

In the following video, we will take you through the basics of what a Git hook is and demonstrate how to use one in GitKraken Desktop.


Where are Git hooks?

Hooks are stored in the hooks subdirectory of the .git directory. This folder is automatically created when you initialize a new repository in GitKraken Desktop and is located in .githooks in your project directory with a README.sample file in it.

Hooks are unique to your local repository and will not be copied over if you create a new repository nor will be tracked by git. Feel free to add, change, or remove scripts from this folder as necessary.


GitKraken Desktop will seamlessly detect any Git hooks in your repository, but if you are running OSX or Linux, you need to give execution rights to the hook file. If you forgot to set your files to executables, GitKraken Desktop will throw an error as a heads up.

Any script that exits with anything other than exit code 0 is considered a fail.


Define a custom hook path

Users can define a custom path for git hooks by going to Preferences Git Hooks. to the location or enter the path to your git hook folder.

This custom git hook path is defined on a per-repository basis.


What hooks are supported by GitKraken Desktop?

Here are the hooks supported by GitKraken Desktop. Where appropriate, beneath each hook are the actions during which GitKraken Desktop calls that hook:

pre-commit

– Commit

– Amend

– Merge Resolve

prepare-commit-msg

– Commit

– Amend

– Cherrypick

– Merge

– Squash

– Revert

commit-msg

– Commit

– Amend

– Merge Resolve

post-commit

– Commit

– Amend

– Cherrypick

– Merge Resolve

– Revert

pre-rebase

– Rebase

– Squash

post-checkout

– Checkout

– Discard Changes (selectively)

post-merge

– Merge (Without Conflicts)

– Fast-Forward

post-rewrite

– Amend

– Squash

– Rebase

pre-push

– Push Branch

– Push Tag

– Delete Remote Branch

– Delete Remote Tag

Git hooks example

Git hooks are scripts that perform automated actions when a specific action is performed in GitKraken Desktop or the command line. The git hook name usually indicates the hook’s trigger (e.g. pre-commit).

Git hooks live under the .git folder of your repo in a directory called hooks. The path to the hooks will look similar to repo/.git/hooks.

Tools needed

Hook Purpose

In this example, we’ll create a pre-commit hook. This hook validates the git config’s global user email. The hook is useful so that the commits contain the correct committer email address.

Creating the git hook

Step 1

First navigate to the hooks directory for the target repo. Open a Visual Studio Code window and navigate to ~/repo/.git/hooks. From here, add a new file to the .git/hooks directory called pre-commit.

Note 📝 – To make the .git folder visible in Visual Studio Code you will need to remove **/.git from files.exclude in the Visual Studio Code settings.

Step 2

Now that we have our pre-commit file, we need to make it executable. To do this we will need the command line.

Open the Gitkraken Terminal window by clicking the Terminal icon in toolbar (or by searching “terminal” in the command palette). Once the terminal is open, change directory to .git/hooks.

Then use the command chmod +x pre-commit to make the pre-commit file executable.

Note 📝 – If you do not have your terminal setup in GitKraken Desktop, please review the Start Here Tips for setup details.

Step 3

Now we create our script using bash. Open the pre-commit file in VS Code as we did before.

The first instruction we need to specify is the shell used in the script, do this by using #!/bin/bash at the beginning of your script for bash or #!/bin/sh if using the sh shell.

This pre-commit hook watches for incorrect commit authors using the script below.

Script variables:
globalEmail – Get email in global git config
workEmail – This is your target email address. The email needed to commit successfully.

In the first condition we validate that the global git config user.email matches with our workEmail. If it fails we will see:

        echo "Commit email and global git config email differ"
        echo "Global commit email: "$globalEmail""
        echo "Committing email expected: $workEmail"
        exit 1

If the condition is successful the script will run and the commit will be made.

Full script

#!/bin/bash

globalEmail=`git config --global --get user.email`
workEmail="[email protected]"

if [[ $globalEmail != $workEmail ]];
then
        echo "Commit email and global git config email differ"
        echo "Global commit email: "$globalEmail""
        echo "Committing email expected: $workEmail"
        exit 1
else
        echo ""
        exit 0
fi

Git hook in action

Environment Variables & Git Hooks

GitKraken uses a non-interactive shell to run its git hooks so you may need to edit your rc profile (e.g. .bashrc or .zshenv) to allow your extensions and/or environment variables to run in non-interactive shells.

Bash shell sources env variables for both interactive and non-interactive definitions from your .bashrc file. But for ZSH, .zshrc is only sourced for interactive shells. Non-interactive definitions for ZSH should go in .zshenv.

Bypass git hooks

There may be times when you want to skip your Git hooks when making a commit. This can be done on a commit-by-commit basis by selecting the Commit and skip hooks option.

Note 📝 – Using this option will bypass all hooks that trigger with git commit action.

Have feedback about this article? Did we miss something? Let us know!
On this page