DEV Community

Per Enström
Per Enström

Posted on • Updated on

Using Husky git hooks with GitKraken

Edit 2021-10-14: A fix for this in GitKraken has been released in version 7.7.2, https://support.gitkraken.com/release-notes/7x/#version-772

Utilising git hooks can be a great way of automating tasks in different parts of the git workflow. This article won’t go into detail about how git hooks work, but if you want a good starting point to learn more you can check out githooks.com, which links a lot of good resources.

A git hook is a file placed in the .git/hooks folder, and named after when in the git lifecycle it should be executed. A common way of simplifying using hooks is to install Husky, which makes the process easier and is often paired with lint-staged.

Since version 5, Husky uses a new git functionality called core.hookspath, which specifies where git should look for hooks. This gives Husky the possibility to create its hooks in a separate directory, which can be checked in and visible. However, this solution can cause problems in git clients that don’t support this feature, for example GitKraken.

To sidestep this problem we can make sure to copy the hooks to the default folder as soon as Husky is set up to make sure that clients that disregard the hookspath still can find the hooks.

By default, Husky adds a prepare script in package.json when it’s set up:

{
  ...
  "scripts": {
    ...
    "prepare": "husky install"
  }
}
Enter fullscreen mode Exit fullscreen mode

This is a standard npm script that runs automatically when running npm install. If we just add a bash command to copy the installed hooks and husky file over to the standard hook folder, we fix our problem with GitKraken.

{
  ...
  "scripts": {
    ...
    "prepare": "husky install && cp -a .husky/. .git/hooks/"
  }
}
Enter fullscreen mode Exit fullscreen mode

Since running npm install is an operation that all developers will start with when cloning the repository, we can make sure that we cover our bases and allow whatever git client the developer wants to use.

Top comments (0)