How to install Git Hooks (overcommit)

April 14, 2020

Even if you install Rubocop in How to install Rubocop in your Rails app, you might forget to run bundle exec rubocop.

Using Git Hooks, you can set it up to automatically run rubocop before you commit.

Install overcommit, which is a useful gem for running Git Hooks.

Install

Gemfile
group :development do
  gem 'overcommit'
end
$ bundle install
$ bundle exec overcommit --install

Settings

Enable Rubocop settings.

.overcommit.yml
RuboCop:
  enabled: true
  command: ['bundle', 'exec', 'rubocop']
  on_warn: fail # Treat all warnings as failures

How to use

Reflect the changes in the settings.

$ bundle exec overcommit --sign

Check the current settings.

$ bundle exec overcommit --list-hooks

References