Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    Startups

    Strategic Approaches to Securing Startup Funding Successfully

    Programming

    10. Уроки Node.js. Node.JS как веб-сервер

    Programming

    3. Express.js Lessons. Templating with EJS: Layout, Block, Partials

    Important Pages:
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Today's Picks:
    • Scaling Success: Monitoring Indexation of Programmatic SEO Content
    • Leveraging Influencers: Key Drivers in New Product Launches
    • How Privacy-First Marketing Will Transform the Industry Landscape
    • The Impact of Social Proof on Thought Leadership Marketing
    • Balancing Value-Driven Content and Promotional Messaging Strategies
    • Top Influencer Marketing Platforms to Explore in 2025
    • Emerging Trends in Marketing Automation and AI Tools for 2023
    • Strategies to Mitigate Duplicate Content in Programmatic SEO
    Wednesday, September 10
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Soshace Digital Blog
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Services
    • SaaS & Tech

      Maximizing Efficiency: How SaaS Lowers IT Infrastructure Costs

      August 27, 2025

      Navigating Tomorrow: Innovations Shaping the Future of SaaS

      August 27, 2025

      Maximizing Impact: Strategies for SaaS & Technology Marketing

      August 27, 2025
    • AI & Automation

      Enhancing Customer Feedback Analysis Through AI Innovations

      August 27, 2025

      Navigating the Impact of AI on SEO and Search Rankings

      August 27, 2025

      5 Automation Hacks Every Home Service Business Needs to Know

      May 3, 2025
    • Finance & Fintech

      Critical Missteps in Finance Marketing: What to Avoid

      August 27, 2025

      Analyzing Future Fintech Marketing Trends: Insights Ahead

      August 27, 2025

      Navigating the Complex Landscape of Finance and Fintech Marketing

      August 27, 2025
    • Legal & Compliance

      Exploring Thought Leadership’s Impact on Legal Marketing

      August 27, 2025

      Maximizing LinkedIn: Strategies for Legal and Compliance Marketing

      August 27, 2025

      Why Transparency Matters in Legal Advertising Practices

      August 27, 2025
    • Medical Marketing

      Enhancing Online Reputation Management in Hospitals: A Guide

      August 27, 2025

      Analyzing Emerging Trends in Health and Medical Marketing

      August 27, 2025

      Exploring Innovative Content Ideas for Wellness Blogs and Clinics

      August 27, 2025
    • E-commerce & Retail

      Strategic Seasonal Campaign Concepts for Online and Retail Markets

      August 27, 2025

      Emerging Trends in E-commerce and Retail Marketing Strategies

      August 27, 2025

      Maximizing Revenue: The Advantages of Affiliate Marketing for E-Commerce

      August 27, 2025
    • Influencer & Community

      Leveraging Influencers: Key Drivers in New Product Launches

      August 27, 2025

      Top Influencer Marketing Platforms to Explore in 2025

      August 27, 2025

      Key Strategies for Successful Influencer Partnership Negotiations

      August 27, 2025
    • Content & Leadership

      The Impact of Social Proof on Thought Leadership Marketing

      August 27, 2025

      Balancing Value-Driven Content and Promotional Messaging Strategies

      August 27, 2025

      Analyzing Storytelling’s Impact on Content Marketing Effectiveness

      August 27, 2025
    • SEO & Analytics

      Scaling Success: Monitoring Indexation of Programmatic SEO Content

      August 27, 2025

      Strategies to Mitigate Duplicate Content in Programmatic SEO

      August 27, 2025

      Effective Data Visualization Techniques for SEO Reporting

      August 27, 2025
    • Marketing Trends

      How Privacy-First Marketing Will Transform the Industry Landscape

      August 27, 2025

      Emerging Trends in Marketing Automation and AI Tools for 2023

      August 27, 2025

      Maximizing ROI: Key Trends in Paid Social Advertising

      August 27, 2025
    Soshace Digital Blog
    Blog / Git / Getting started with Git Hooks using ghooks
    Git

    Getting started with Git Hooks using ghooks

    dillionmegidaBy dillionmegidaAugust 4, 2020Updated:May 26, 2024No Comments8 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    Getting started with Git Hooks using ghooks
    Getting Started with Github Hooks using Ghooks
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link
    Getting Started with Github Hooks using Ghooks
    Getting Started with Github Hooks using Ghooks

    As a developer working on your own or with people, you must have used version control for your projects. It helps you keep track of the progress and changes in your applications and you can also return to previous versions.

    When you build projects on your own, it’s very easy to maintain compared to when you have many contributors. The maintenance becomes hard in terms of the style guide, applying tests, and many other rules you may want your application to follow.

    Moreover, there are also repetitive tasks you would be doing (linting and testing your applications) even when working on them on your own. This can be tiring, and sometimes you might just forget.

    Git Hooks helps to trigger commands (could be something repetitive you want to be automated) and ghooks makes working with Git Hooks easier as well as syncing with other contributors to your application.

    Have you ever tried intercepting a merge operation? Or a commit operation (for yourself or others)? Or a push operation? Or ensuring something happens automatically when you’re trying to git?

    When these operations occur, your application and git records are likely transformed into a new state.

    Git hooks provide a means to intercept these operations or automate some after-action. These interceptions do not necessarily stop the operation but ensure that some standards (as you have specified) are followed or some things are done afterward.

    What are Git hooks

    Git hooks are scripts that are executed when a git operation is triggered. Some of them are before an operation, while others, after. Here’s an illustration:

    Read More:  Programming Patterns. SOLID principle

    Illustrating what git hooks are and when they are called.
    Illustrating what git hooks are and when they are called.

    Git hooks are found in the hooks directory under .git. When a particular operation is run, git checks the related hook in this directory to know the script to execute.

    If you have a project where you use git, you can check the `.git` directory. You’ll find something similar to this:

    Screenshot of git pre-populated hooks folder
    Screenshot of git pre-populated hooks folder

    When you open each hook, you’ll find codes similar to bash scripts. However, any executable script (like Python or Node.js) should work.

    If you find the .sample extension on the files like above, know that it exists so that git does not run them. They are just sample scripts populated by git on initialization.

    Let’s try one of them. Remove the .sample extension from pre-commit, clear the file and add the following Node.js script (Node.js must be installed):

    #!/usr/bin/env node
    console.log('hello')

    #!/usr/bin/env node is referred to as a shebang which shows the path to the interpreter that would execute the script.

    To test this, make any change, and try to commit the change, you should see “hello” logged to the console (by Node.js). We’ll talk more on pre-commit`later, but for now, just understand that this hook is run before a commit is made.

    Types of Git Hooks

    1. Client-side Hooks

    For the client-side, the hooks are executed on a local repository. For example, a pre-commit hook is run before a commit operation completes.

    2. Server-side Hooks

    These types of hooks are run on the server of the remote repository. For example, a post-receive hook is run after a push operation.

    If any of git pre-hooks (executed before an operation) is executed and returns a 0 exit code, the operation being run passes, else, the operation is aborted. The process is described in this image:

    Illustrating how operations are aborted when git hooks throw errors
    Illustrating how operations are aborted when git hooks throw errors

    Note that, there can exist more than one hook for a type of operation. For instance, the hooks related to commit operations are commit-msg, pre-commit, and more. Therefore, you’d need to understand which is which.

    For the rest of this article, we’ll focus on client-side hooks to get a clearer picture of how they work and how to use them.

    Examples of Git Hooks

    There are numerous types of hooks. We’ll look at a few of them.

    1. pre-commit: this hook is executed before the commit editor is opened (or a commit object created). That is when you execute git commit, this hook is run before the editor opens. Similarly, when you execute git commit -m "commit message", the hook is run before -m commit message is applied.
    2. post-commit: is executed after the commit editor closes and the commit object is created.
    3. pre-push: is executed before a push operation completes, that is, before objects have been transferred to the remote repository.
    4. pre-receive: is executed before the remote repository receives a push operation.

    You can find the different types of hooks in this documentation.

    Use cases of Git hooks

    1. Ensure that code follows linting configurations before committing.
    2. Test your code before committing.
    3. Test your code after merging.

    There’s more! These are just a few and I consider them popular. You can think of other things like sending mail notifications of the last commit, and more.

    Later in this article, we’ll learn exactly how to implement these mentioned use cases.

    Scope of Client-side hooks

    These hooks are scoped to a local git repository. This means that when you push changes (after configuring .git/hooks) to the remote repository and you (or anyone else) clones the repository, the initial pre-populated scripts are present.

    This becomes difficult to ensure policies across many local repositories of a project. ghooks solves this problem by configuring package.json so that on installing the package, the hooks folder for your project is populated and you can trigger some commands that will apply to all repositories of a project.

    We’ll learn more of ghooks achieves this in the next section.

    Customizing Git hooks

    ghooks makes customizing git hooks seamlessly easier for developers. It configures the git hooks under the hooks directory. With that, we can provide commands that will run at specific operations.

    With a little configuration, you can begin executing commands based on a git operation. Let’s see how with a few examples.

    Setting up ghooks

    Firstly, install the library:

    npm install ghooks --save-dev

    After installation, ghooks will configure the hooks in the hooks directory of git so that you can easily run your commands specified commands at specific action points of git operations.

    Secondly, Add ghooks to package.json

    Add ghooks to the config property in package.json like so:

    {
      "config": {
        "ghooks": {
          // githook: command
        }
      }
    }

    That’s all there is to use ghooks. Let’s get started using it.

    Setting up ghooks for mentioned the use cases

    1. Ensure compliance with linting configurations before committing

    For this example, we’d assume we have eslint configured for our application like so:

    {
      "script": {
        "lint": "eslint ."
      },
    }

    With this hook, you can run the eslint command when a commit operation is about to be executed. That action would be configured like so:

    {
      "script": {
        "lint": "eslint ."
      },
      "config": {
        "ghooks": {
          "pre-commit": "npm run lint"
        }
      }
    }

    With the above setup, git will trigger npm lint when you’re about to commit staged changes. This way, you can immediately get an error for code that doesn’t comply with your eslint configurations.

    2 .Execute tests before committing

    Normally after setting up test (say mocha), you’d have to run npm run test. Without git hooks, you may forget to run tests before committing. Tjat would be bad for your application.

    Similar to the above configuration for linting, we would have:

    {
      "script": {
        "test": "mocha ."
      },
      "config": {
        "ghooks": {
          "pre-commit": "npm run test"
        }
      }
    }

    3. Test your code after merging

    Just like our test script above, we can have:

    {
      "script": {
        "test": "mocha ."
      },
      "config": {
        "ghooks": {
          "post-merge": "npm run test"
        }
      }
    }

    This would return the test script after a merge operation is carried. This would help you focus on the parts of your application that needs attention before you start adding new features.

    Wrap up

    The use cases mentioned may look simple. These are very common cases. Different hooks can be used for different purposes. The customizability depends on how creative the developer can be. Check out this list of hooks to see when git triggers them so you can run your commands.

    Git hooks make life easier for developers. It creates an automated flow of development that can save developers of errors and ensure standards in an application.

    If I, as a contributor were to make changes to your application, these customized hooks will ensure that all requirements are followed.

    By using githooks, you can create applications where people can contribute without breaking your application. Ghooks even makes it very easy.

    I hope this getting started guide, gets you started using git hooks and ghooks.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    dillionmegida

      Related Posts

      Mastering REST APIs: Essential Techniques for Programmers

      December 18, 2024

      Crafting Interactive User Interfaces Using JavaScript Techniques

      December 17, 2024

      Effective Strategies for Utilizing Frameworks in Web Development

      December 16, 2024
      Leave A Reply Cancel Reply

      You must be logged in to post a comment.

      Stay In Touch
      • Facebook
      • Twitter
      • Pinterest
      • Instagram
      • YouTube
      • Vimeo
      Don't Miss
      JavaScript October 23, 2020

      Create simple POS with React.js, Node.js, and MongoDB #17: Stat screen

      Now, we are going to implement the final chapter for this tutorial series where we are implementing a graphical representation section on our dashboard screen. In the dashboard screen, we are going o display the statistics from the database using the react-chart.js library.

      Disadvantages of Using TypeScript

      May 31, 2019

      Safeguarding Innovation: A Guide to Startup Intellectual Property

      December 16, 2024

      Top 5 Free Website Builders in 2019

      October 14, 2019

      Categories

      • AI & Automation
      • Angular
      • ASP.NET
      • AWS
      • B2B Leads
      • Beginners
      • Blogs
      • Business Growth
      • Case Studies
      • Comics
      • Consultation
      • Content & Leadership
      • CSS
      • Development
      • Django
      • E-commerce & Retail
      • Entrepreneurs
      • Entrepreneurship
      • Events
      • Express.js
      • Facebook Ads
      • Finance & Fintech
      • Flask
      • Flutter
      • Franchising
      • Funnel Strategy
      • Git
      • GraphQL
      • Home Services Marketing
      • Influencer & Community
      • Interview
      • Java
      • Java Spring
      • JavaScript
      • Job
      • Laravel
      • Lead Generation
      • Legal & Compliance
      • LinkedIn
      • Machine Learning
      • Marketing Trends
      • Medical Marketing
      • MSP Lead Generation
      • MSP Marketing
      • NestJS
      • Next.js
      • Node.js
      • Node.js Lessons
      • Paid Advertising
      • PHP
      • Podcasts
      • POS Tutorial
      • Programming
      • Programming
      • Python
      • React
      • React Lessons
      • React Native
      • React Native Lessons
      • Recruitment
      • Remote Job
      • SaaS & Tech
      • SEO & Analytics
      • Soshace
      • Startups
      • Swarm Intelligence
      • Tips
      • Trends
      • Vue
      • Wiki
      • WordPress
      Top Posts

      Node.js Lesson 17: Timers, Differences from Browser, ref and ref

      Node.js March 9, 2021

      Nodejs Lesson 15: Internals of Nodejs: LibUV

      Node.js February 4, 2021

      Freelance Programming Is Great — but Is It for You?

      Remote Job September 27, 2019

      A Guide to HR Developing Performance Goals

      Franchising January 17, 2019

      Subscribe to Updates

      Get The Latest News, Updates, And Amazing Offers

      About Us
      About Us

      Soshace Digital delivers comprehensive web design and development solutions tailored to your business objectives. Your website will be meticulously designed and developed by our team of seasoned professionals, who combine creative expertise with technical excellence to transform your vision into a high-impact, user-centric digital experience that elevates your brand and drives measurable results.

      7901 4th St N, Suite 28690
      Saint Petersburg, FL 33702-4305
      Phone: 1(877)SOSHACE

      Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
      Our Picks
      JavaScript

      Are Coding Bootcamps Worth Your Time? Yes — with the Right Approach

      Interview

      Best Resources for Preparing for Your Technical Interview: Books and Online Platforms

      Trends

      Modern Recruitment Strategies | Fair Hiring Practices | Helpful Tools & Practical Advice

      Most Popular

      Server-Side Rendering with Vue.js and Nuxt.js

      JavaScript

      Strategic LinkedIn Techniques for Real Estate Lead Generation

      LinkedIn

      Why Fastify is a better Nodejs framework for your next project compared to Express

      Node.js
      © 2025 Soshace Digital.
      • Home
      • About
      • Services
      • Contact Us
      • Privacy Policy
      • Terms & Conditions

      Type above and press Enter to search. Press Esc to cancel.