{"id":1399,"date":"2024-09-08T00:00:00","date_gmt":"2024-09-07T22:00:00","guid":{"rendered":"https:\/\/demo.sidekick.be\/itenium\/git-hooks-with-husky-v9\/"},"modified":"2025-11-28T13:31:42","modified_gmt":"2025-11-28T12:31:42","slug":"git-hooks-with-husky-v9","status":"publish","type":"post","link":"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/git-hooks-with-husky-v9\/","title":{"rendered":"Git Hooks with Husky v9"},"content":{"rendered":"<h1 id=\"hooks\">Hooks<\/h1>\n<blockquote>\n<p><strong>git hooks<\/strong><br \/> Run a program before certain points in gits execution.<br \/><code>pre-commit<\/code> and <code>pre-push<\/code> hooks are probably the most interesting.<\/p>\n<\/blockquote>\n<p>They are defined in <code>.git\/hooks<\/code>. You can find some example files in that directory. Remove their <code>.sample<\/code> suffix to test them out.<\/p>\n<p>The githooks are saved locally and not pushed to the remote. This is obviously a problem when working in a team but the <code>.git\/hooks<\/code> folder can be changed globally or for the current repository with the <a href=\"https:\/\/git-scm.com\/docs\/git-config#Documentation\/git-config.txt-corehooksPath\"><code>core.hooksPath<\/code><\/a> setting.<\/p>\n<p>This is obviously never going to happen but you could skip githooks with:<\/p>\n<pre><code class=\"language-sh\">git commit --no-verify\ngit push --no-verify\n\n# Or if you want to really quickly skip it ;)\ngit commit -n\n<\/code><\/pre>\n<h1 id=\"husky\">Husky<\/h1>\n<p>Or, use Husky to easily make the git hooks part of the repository itself and share it with the entire team.<\/p>\n<pre><code class=\"language-sh\">npm install husky --save-dev\nnpx husky init\n<\/code><\/pre>\n<p>This will add a <code>prepare<\/code> npm script in your <code>package.json<\/code>. The prepare script runs after an <code>npm install<\/code> to ensure things are setup for everyone.<\/p>\n<pre><code class=\"language-json\">\"scripts\": {\n  \"prepare\": \"husky\"\n},\n<\/code><\/pre>\n<p>And it creates a <code>.husky\/pre-commit<\/code> which you can try out by doing a <code>git commit<\/code>!<br \/> Avoid having to <code>git reset HEAD~<\/code> all the time by ending your script with <code>exit 1<\/code> while testing\/setting it up.<\/p>\n<p>Finally it sets the <code>core.hooksPath<\/code> to <code>.husky\/_<\/code>. Husky creates files in that directory to link all githooks with the <code>h<\/code> shell script which will execute a <code>.husky\/SOME_GITHOOK<\/code> if it exists.<br \/> Husky also adds a gitignore to the <code>_<\/code> folder to keep the generated files out of your git repository.<\/p>\n<pre><code class=\"language-sh\">git config core.hooksPath\n# --&gt; .husky\/_\n<\/code><\/pre>\n<h2 id=\"script-h\">Script <code>h<\/code><\/h2>\n<p>In this order:<\/p>\n<ul>\n<li>If <code>HUSKY=2<\/code> it executes <code>set -x<\/code> to print commands with arguments<\/li>\n<li>If the <code>.husky\/XXX<\/code> file does not exist (with XXX for example <code>pre-commit<\/code>), the script exits<\/li>\n<li>If <code>~\/.config\/husky\/init.sh<\/code> (or <code>$XDG_CONFIG_HOME\/husky\/init.sh<\/code>) exists, it executes it\n<ul>\n<li>This is the place to setup <code>PATH<\/code>, or set an environment variable like <code>NVM_DIR<\/code><\/li>\n<\/ul>\n<\/li>\n<li>If <code>HUSKY=0<\/code> the script exits<\/li>\n<li>It adds <code>node_modules\/.bin<\/code> to the <code>PATH<\/code><\/li>\n<li>It executes <code>.husky\/XXX<\/code> (stops on first error)<\/li>\n<\/ul>\n<h2 id=\"cicd\">CI\/CD<\/h2>\n<p>You may not be installing the <code>devDependencies<\/code> during a production build. To avoid the <code>prepare<\/code> npm script from failing:<\/p>\n<pre><code class=\"language-json\">\"scripts\": {\n  \"prepare\": \"husky || true\"\n},\n<\/code><\/pre>\n<h2 id=\"project-not-in-git-root-directory\">Project Not in Git Root Directory<\/h2>\n<p>Prepush script that works around the frontend project <code>package.json<\/code> not being in the root of the git repository.<\/p>\n<p>Set the <code>package.json<\/code> script manually and then do an <code>npm install<\/code>.<\/p>\n<pre><code class=\"language-json\">\"scripts\": {\n  \"prepare\": \"cd .. &amp;&amp; husky frontend\/.husky\"\n},\n<\/code><\/pre>\n<p>The script with some path juggling:<\/p>\n<pre><code class=\"language-bash\">#!\/bin\/sh\n\n# Path Juggling\noriginalPath=`pwd`\nif [ \"`git rev-parse --show-cdup`\" != \"\" ]; then cd `git rev-parse --show-cdup`; fi\ncd .\/frontend-project-folder\/\n\n# Linting\nnpm run lint\nif [ $? -ne 0 ]; then\n    cd $originalPath\n    exit -1\nfi\n\n# Testing\nnpm test\nif [ $? -ne 0 ]; then\n  cd $originalPath\n  exit -1\nfi\n\n# Build\nnpm run build\nhasBuildErrors=$?\n\ncd $originalPath\nexit $hasBuildErrors\n<\/code><\/pre>\n<h1 id=\"alternatives\">Alternatives<\/h1>\n<p><a href=\"https:\/\/github.com\/toplenboren\/simple-git-hooks\" class=\"github\"><img decoding=\"async\" src=\"https:\/\/img.shields.io\/github\/stars\/toplenboren\/simple-git-hooks.svg?style=social&amp;label=Star\" \/><\/a><a href=\"https:\/\/github.com\/toplenboren\/simple-git-hooks\">toplenboren\/simple-git-hooks<\/a>: A simple git hooks manager for small projects <a href=\"https:\/\/github.com\/evilmartians\/lefthook\" class=\"github\"><img decoding=\"async\" src=\"https:\/\/img.shields.io\/github\/stars\/evilmartians\/lefthook.svg?style=social&amp;label=Star\" \/><\/a><a href=\"https:\/\/github.com\/evilmartians\/lefthook\">evilmartians\/lefthook<\/a>: Fast and powerful Git hooks manager for any type of projects <a href=\"https:\/\/github.com\/pre-commit\/pre-commit\" class=\"github\"><img decoding=\"async\" src=\"https:\/\/img.shields.io\/github\/stars\/pre-commit\/pre-commit.svg?style=social&amp;label=Star\" \/><\/a><a href=\"https:\/\/github.com\/pre-commit\/pre-commit\">pre-commit\/pre-commit<\/a>: A framework for managing and maintaining multi-language pre-commit hooks<\/p>\n<h1>Extras<\/h1>\n<p><b>Stuff that came into being during the making of this post<\/b><\/p>\n<ul>\n<li><a href=\"https:\/\/github.com\/itenium-be\/Frontend-Track\">Template code repository<\/a><\/li>\n<\/ul>\n<p><b>Other interesting reads<\/b><\/p>\n<ul>\n<li><a href=\"https:\/\/git-scm.com\/docs\/githooks\">Official githooks documentation<\/a><\/li>\n<\/ul>\n<h1> Package Versions <i class=\"fa fa-info-circle\" style=\"float: right; color: cornflowerblue; font-size: 28px\" title=\"Package versions used for this post and in the accompanying code on Github\"><\/i><\/h1>\n<div><a href=\"https:\/\/github.com\/typicode\/husky\" class=\"github\"><img decoding=\"async\" src=\"https:\/\/img.shields.io\/github\/stars\/typicode\/husky.svg?style=social&#038;label=Star\" \/><\/a><a href=\"https:\/\/github.com\/typicode\/husky\">typicode\/husky<\/a>: v9.1.5 <\/div>\n","protected":false},"excerpt":{"rendered":"<p><a href=\"https:\/\/github.com\/typicode\/husky\" class=\"github\"><img decoding=\"async\" src=\"https:\/\/img.shields.io\/github\/stars\/typicode\/husky.svg?style=social&amp;label=Star\" \/><\/a><a href=\"https:\/\/github.com\/typicode\/husky\">typicode\/husky<\/a>: Git hooks made easy \ud83d\udc36 woof!<\/p>\n","protected":false},"author":1,"featured_media":1985,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[5],"tags":[26],"class_list":["post-1399","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dev-setup","tag-git"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Git Hooks with Husky v9 - Itenium<\/title>\n<meta name=\"robots\" content=\"noindex, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Git Hooks with Husky v9 - Itenium\" \/>\n<meta property=\"og:description\" content=\"typicode\/husky: Git hooks made easy \ud83d\udc36 woof!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/git-hooks-with-husky-v9\/\" \/>\n<meta property=\"og:site_name\" content=\"Itenium\" \/>\n<meta property=\"article:published_time\" content=\"2024-09-07T22:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-11-28T12:31:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/git-hooks-husky-v9.png\" \/>\n\t<meta property=\"og:image:width\" content=\"360\" \/>\n\t<meta property=\"og:image:height\" content=\"300\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Usdevelop\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Usdevelop\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/git-hooks-with-husky-v9\/\",\"url\":\"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/git-hooks-with-husky-v9\/\",\"name\":\"Git Hooks with Husky v9 - Itenium\",\"isPartOf\":{\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/git-hooks-with-husky-v9\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/git-hooks-with-husky-v9\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/git-hooks-husky-v9.png\",\"datePublished\":\"2024-09-07T22:00:00+00:00\",\"dateModified\":\"2025-11-28T12:31:42+00:00\",\"author\":{\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/#\/schema\/person\/6ab401ce01e858184ee4a8667fe0f022\"},\"breadcrumb\":{\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/git-hooks-with-husky-v9\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/git-hooks-with-husky-v9\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/git-hooks-with-husky-v9\/#primaryimage\",\"url\":\"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/git-hooks-husky-v9.png\",\"contentUrl\":\"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/git-hooks-husky-v9.png\",\"width\":360,\"height\":300,\"caption\":\"husky git hooks\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/git-hooks-with-husky-v9\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/demo.sidekick.be\/itenium\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Git Hooks with Husky v9\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/#website\",\"url\":\"https:\/\/demo.sidekick.be\/itenium\/\",\"name\":\"Itenium\",\"description\":\"We build software. We build careers.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/demo.sidekick.be\/itenium\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/#\/schema\/person\/6ab401ce01e858184ee4a8667fe0f022\",\"name\":\"Usdevelop\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cc8cf9b9e9d5bbe7e8d247907cc9ac8457bbfa2854b52eaddf6529adfe88ceab?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cc8cf9b9e9d5bbe7e8d247907cc9ac8457bbfa2854b52eaddf6529adfe88ceab?s=96&d=mm&r=g\",\"caption\":\"Usdevelop\"},\"sameAs\":[\"https:\/\/demo.sidekick.be\/itenium\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Git Hooks with Husky v9 - Itenium","robots":{"index":"noindex","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"en_US","og_type":"article","og_title":"Git Hooks with Husky v9 - Itenium","og_description":"typicode\/husky: Git hooks made easy \ud83d\udc36 woof!","og_url":"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/git-hooks-with-husky-v9\/","og_site_name":"Itenium","article_published_time":"2024-09-07T22:00:00+00:00","article_modified_time":"2025-11-28T12:31:42+00:00","og_image":[{"width":360,"height":300,"url":"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/git-hooks-husky-v9.png","type":"image\/png"}],"author":"Usdevelop","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Usdevelop","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/git-hooks-with-husky-v9\/","url":"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/git-hooks-with-husky-v9\/","name":"Git Hooks with Husky v9 - Itenium","isPartOf":{"@id":"https:\/\/demo.sidekick.be\/itenium\/#website"},"primaryImageOfPage":{"@id":"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/git-hooks-with-husky-v9\/#primaryimage"},"image":{"@id":"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/git-hooks-with-husky-v9\/#primaryimage"},"thumbnailUrl":"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/git-hooks-husky-v9.png","datePublished":"2024-09-07T22:00:00+00:00","dateModified":"2025-11-28T12:31:42+00:00","author":{"@id":"https:\/\/demo.sidekick.be\/itenium\/#\/schema\/person\/6ab401ce01e858184ee4a8667fe0f022"},"breadcrumb":{"@id":"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/git-hooks-with-husky-v9\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/git-hooks-with-husky-v9\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/git-hooks-with-husky-v9\/#primaryimage","url":"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/git-hooks-husky-v9.png","contentUrl":"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/git-hooks-husky-v9.png","width":360,"height":300,"caption":"husky git hooks"},{"@type":"BreadcrumbList","@id":"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/git-hooks-with-husky-v9\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/demo.sidekick.be\/itenium\/"},{"@type":"ListItem","position":2,"name":"Git Hooks with Husky v9"}]},{"@type":"WebSite","@id":"https:\/\/demo.sidekick.be\/itenium\/#website","url":"https:\/\/demo.sidekick.be\/itenium\/","name":"Itenium","description":"We build software. We build careers.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/demo.sidekick.be\/itenium\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/demo.sidekick.be\/itenium\/#\/schema\/person\/6ab401ce01e858184ee4a8667fe0f022","name":"Usdevelop","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/demo.sidekick.be\/itenium\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cc8cf9b9e9d5bbe7e8d247907cc9ac8457bbfa2854b52eaddf6529adfe88ceab?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cc8cf9b9e9d5bbe7e8d247907cc9ac8457bbfa2854b52eaddf6529adfe88ceab?s=96&d=mm&r=g","caption":"Usdevelop"},"sameAs":["https:\/\/demo.sidekick.be\/itenium"]}]}},"_links":{"self":[{"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/posts\/1399","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/comments?post=1399"}],"version-history":[{"count":3,"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/posts\/1399\/revisions"}],"predecessor-version":[{"id":2259,"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/posts\/1399\/revisions\/2259"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/media\/1985"}],"wp:attachment":[{"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/media?parent=1399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/categories?post=1399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/tags?post=1399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}