Skip to content

Instantly share code, notes, and snippets.

@rodrigopinto
Last active January 18, 2023 15:20
Show Gist options
  • Save rodrigopinto/b34dff4ad430806233b0789c94ddedbb to your computer and use it in GitHub Desktop.
Save rodrigopinto/b34dff4ad430806233b0789c94ddedbb to your computer and use it in GitHub Desktop.
Runs rubocop against the diff before commit

For those like me that often forget to run rubocop against the diff code, I added a pre-commit git hook to do this.

  1. Goes to your repository folder $ cd yourrepo/.

  2. Add the pre-commit executable file to your .git/hooks folder.

  3. Be happy, you will never more be alerted by your CI about rubocop complaints.

#!/bin/sh
#
# Runs rubocop automatically on the diff before allowing to commit
# for unstaged files
git diff --name-only | xargs rubocop
# for staged files
git diff --staged --name-only | xargs rubocop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment