<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Gabriel Gonzalez</title>
    <description>The latest articles on DEV Community by Gabriel Gonzalez (@hggonzalezdev).</description>
    <link>https://dev.to/hggonzalezdev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F278247%2F5ca56385-bd84-470d-9e9f-84fcbb7c8c28.jpeg</url>
      <title>DEV Community: Gabriel Gonzalez</title>
      <link>https://dev.to/hggonzalezdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hggonzalezdev"/>
    <language>en</language>
    <item>
      <title>Get Started with Git Hooks and Husky</title>
      <dc:creator>Gabriel Gonzalez</dc:creator>
      <pubDate>Thu, 12 Aug 2021 03:18:31 +0000</pubDate>
      <link>https://dev.to/hggonzalezdev/get-started-with-git-hooks-and-husky-2c3e</link>
      <guid>https://dev.to/hggonzalezdev/get-started-with-git-hooks-and-husky-2c3e</guid>
      <description>&lt;h2&gt;
  
  
  What are Git Hooks?, How they work?, How can I use then? 😕
&lt;/h2&gt;

&lt;p&gt;Those are a few question I'm gonna try to answer in this post.&lt;/p&gt;

&lt;p&gt;As you may know Git is a version control system and like many other control systems it has some predetermined custom scripts to run certain kind of operations, these are called hooks and they are divided into two categories.&lt;/p&gt;

&lt;h2&gt;
  
  
  Client Side
&lt;/h2&gt;

&lt;p&gt;This hooks are triggered by operations such as committing or merging, between this hooks we have &lt;code&gt;pre-commit&lt;/code&gt;, &lt;code&gt;prepare-commit-msg&lt;/code&gt;, &lt;code&gt;commit-msg&lt;/code&gt; just to name a few.&lt;/p&gt;

&lt;h2&gt;
  
  
  Server Side
&lt;/h2&gt;

&lt;p&gt;This hooks run only on network operations such as receiving pushed commits, in this hooks you can find &lt;code&gt;pre-receive&lt;/code&gt;, &lt;code&gt;update&lt;/code&gt; and &lt;code&gt;post-receive&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Install Git Hooks
&lt;/h2&gt;

&lt;p&gt;Hooks live inside the &lt;code&gt;.git/hooks&lt;/code&gt; directory of every git repository. Git automatically populate this folder with some example scripts, if you navigate to the &lt;code&gt;.git/hooks&lt;/code&gt; directory you will see the following files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;applypatch-msg.sample   fsmonitor-watchman.sample
pre-applypatch.sample   prepare-commit-msg.sample
pre-rebase.sample       update.sample
commit-msg.sample       post-update.sample
pre-commit.sample       pre-push.sample
pre-receive.sample
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are the most common hooks available,as you can see they all have a &lt;code&gt;.sample&lt;/code&gt; extension, what this those is that it prevents them from executing by default, to "install" a hook all you need to do is remove the &lt;code&gt;.sample&lt;/code&gt; extension.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hooks Scope
&lt;/h2&gt;

&lt;p&gt;Hooks are local to any git repository, this means they are &lt;strong&gt;not&lt;/strong&gt; copied into another repository when you run &lt;code&gt;git clone&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This represents a big impact when you try to configure hooks for a team of developers, just the simple configuration can be tricky since you have to take into account that &lt;code&gt;.git/hooks&lt;/code&gt; directory isn't clone with the rest of your project, nor is it under version control, a simple solution will be to place the hooks in the root of your project (above the .git directory).&lt;/p&gt;

&lt;h2&gt;
  
  
  What Can You Use Hooks For
&lt;/h2&gt;

&lt;p&gt;You can use hooks to do pretty much everything you want, for example let's say we are working with the &lt;code&gt;pre-commit&lt;/code&gt; hook (this script runs every time you run the &lt;code&gt;git commit&lt;/code&gt; before git ask the developer for a commit message). You can use this hook to inspect the snapshot is about to be committed, you may want to run some automated test to make sure the commit doesn't break any existing functionality.&lt;/p&gt;

&lt;h2&gt;
  
  
  Husky
&lt;/h2&gt;

&lt;p&gt;Now you might be wondering if there is a simple way to do all that, based on everything I told you you might think is not worth the effort since is way to complicated, well don't sweat it since husky is here to make your life more easy 🐶 woof!&lt;/p&gt;

&lt;h3&gt;
  
  
  Install Husky
&lt;/h3&gt;

&lt;p&gt;In previous versions of husky (v4) all you need it to do was to add husky as a dev dependecy by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#NPM&lt;/span&gt;
npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save-dev&lt;/span&gt; husky

&lt;span class="c"&gt;#Yarn&lt;/span&gt;
yarn add &lt;span class="nt"&gt;--dev&lt;/span&gt; husky
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then you need it to add the following inside your &lt;code&gt;package.json&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
"husky": {
   "hooks": {
      "pre-commit": "echo ”[Husky] pre-commit”"
   }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once that setup was complete you were ready to go. Since the release of version 6, things are a little different&lt;/p&gt;

&lt;h4&gt;
  
  
  Automatic (recommended)
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#NPM&lt;/span&gt;
npx husky-init &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; npm &lt;span class="nb"&gt;install&lt;/span&gt;

&lt;span class="c"&gt;#Yarn 1&lt;/span&gt;
npx husky-init &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; yarn

&lt;span class="c"&gt;#Yarn 2&lt;/span&gt;
yarn dlx husky-init &lt;span class="nt"&gt;--yarn2&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; yarn
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a directory in the root of your project called .husky, inside of it you will find the pre-commit hook with the instruction to run &lt;code&gt;npm test&lt;/code&gt;, you can update that instructions to &lt;code&gt;yarn test&lt;/code&gt; if you are using yarn.&lt;/p&gt;

&lt;p&gt;What this will do is to run the test script "if you have any" before it even lets you create a commit message. Keep in mind that you inside the pre-commit hook you can run whatever you want, you can run a linter or anything that comes to your mind.&lt;/p&gt;

&lt;p&gt;To add new hooks you can do it by using &lt;code&gt;husky add&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx husky add .husky/commit-msg &lt;span class="s1"&gt;'npx --no-install commitlint --edit "$1"'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Manual
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Install
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#NPM&lt;/span&gt;
npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save-dev&lt;/span&gt; husky

&lt;span class="c"&gt;#Yarn&lt;/span&gt;
yarn add &lt;span class="nt"&gt;--dev&lt;/span&gt; husky
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Enable git hooks
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx husky &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;To automatically have then enabled after install you need to add the following script to your &lt;code&gt;package.json&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
"prepare": "husky install"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Yarn 2 does not support &lt;code&gt;prepare&lt;/code&gt; lifecycle script, so you need to install husky differently&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;To add or create a new hook you can use the following: &lt;code&gt;husky add &amp;lt;file&amp;gt; [cmd]&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx husky add .husky/pre-commit &lt;span class="s2"&gt;"npm test"&lt;/span&gt;
git add .husky/pre-commit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now if you try to commit your new changes, lets say for example &lt;code&gt;git commit -m "keep it clean"&lt;/code&gt; the hook should trigger and if &lt;code&gt;npm test&lt;/code&gt; fails, your commit will be automatically aborted.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Bonus
&lt;/h2&gt;

&lt;p&gt;If you are using &lt;strong&gt;Yarn 2&lt;/strong&gt; the installation is a little different than with &lt;em&gt;npm&lt;/em&gt; and &lt;em&gt;Yarn 1&lt;/em&gt;, here are the steps you should follow when working with Yarn 2.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install Husky
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn add husky &lt;span class="nt"&gt;--dev&lt;/span&gt;
yarn add pinst &lt;span class="nt"&gt;--dev&lt;/span&gt; &lt;span class="c"&gt;# ONLY if your package is not private&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Enable Git Hooks
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn husky &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;To automatically have then enabled after install, edit your &lt;code&gt;package.json&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;// package.json
&lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="s2"&gt;"private"&lt;/span&gt;: &lt;span class="nb"&gt;true&lt;/span&gt;, // ← your package is private, you only need postinstall
  &lt;span class="s2"&gt;"scripts"&lt;/span&gt;: &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"postinstall"&lt;/span&gt;: &lt;span class="s2"&gt;"husky install"&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;If your package is not private, add the following instead&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;// package.json
&lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="s2"&gt;"private"&lt;/span&gt;: &lt;span class="nb"&gt;false&lt;/span&gt;, // ← your package is public
  &lt;span class="s2"&gt;"scripts"&lt;/span&gt;: &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"postinstall"&lt;/span&gt;: &lt;span class="s2"&gt;"husky install"&lt;/span&gt;,
    &lt;span class="s2"&gt;"prepublishOnly"&lt;/span&gt;: &lt;span class="s2"&gt;"pinst --disable"&lt;/span&gt;,
    &lt;span class="s2"&gt;"postpublish"&lt;/span&gt;: &lt;span class="s2"&gt;"pinst --enable"&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keep Coding 💻&lt;br&gt;
Keep Learning 📖&lt;/p&gt;

&lt;p&gt;Hope this can help you, and now you have a more clear understanding of what are Git Hooks, how they work and what is Husky 🐶&lt;/p&gt;

&lt;p&gt;Here are some useful link you should check out&lt;/p&gt;

&lt;p&gt;&lt;a href="https://typicode.github.io/husky"&gt;https://typicode.github.io/husky&lt;/a&gt;&lt;br&gt;
&lt;a href="https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks"&gt;https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.atlassian.com/git/tutorials/git-hooks"&gt;https://www.atlassian.com/git/tutorials/git-hooks&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/conventional-changelog/commitlint"&gt;https://github.com/conventional-changelog/commitlint&lt;/a&gt;&lt;/p&gt;

</description>
      <category>git</category>
      <category>husky</category>
    </item>
  </channel>
</rss>
