How to add Git hooks to a Ruby project using Overcommit

What is a Git hook?

A Git hook is essentially some bit of code/script that you run when something happens in Git. A very common example of a hook is running a linter like Rubocop before each Git commit.

What is Overcommit?

Overcommit is a tool that makes managing your hooks less of a pain in the ass.

Installation

Add to your Gemfile and run bundle

group :development, :test do
  gem 'overcommit'
emd

Install the hooks for your project and generate a .overcommit.yml config file:

overcommit --install
overcommit --sign # May need to run if you get an error

Add a hook

Let’s setup a pre-commit hook to run Rubocop. Edit your .overcommit.yml file:

PreCommit:
  RuboCop:
    enabled: true
    on_warn: fail # Treat all warnings as failures

Overcommit has a bunch of built-in hooks but you can also add custom hooks if needed.

List hooks in project

overcommit -l

Voila!

Sajad Torkamani

Hello! My name is Sajad. I’m a software developer at Nationwide and an aspiring entrepreneur. Originally from Afghanistan, I moved to London at an early age and have been living here for the past 20 years.

This is my personal website where I blog mostly about programming with occasional thoughts on a bunch of other things. I write mainly for my own benefit as I find that writing helps clarify my otherwise very confused thoughts.

Get in touch: sajadtorkamani1@gmail.com