Copy this into .git/hooks/pre-commit (remove .sample)
#!/usr/bin/env ruby
require 'english'
require 'rubocop'
ADDED_OR_MODIFIED = /A|AM|^M/.freeze
changed_files = `git status --porcelain`.split(/\n/).
select { |file_name_with_status|
file_name_with_status =~ ADDED_OR_MODIFIED
}.
map { |file_name_with_status|
file_name_with_status.split(' ')[1]
}.
select { |file_name|
File.extname(file_name) == '.rb'
}.join(' ')
system("rubocop #{changed_files}") unless changed_files.empty?
exit $CHILD_STATUS.exitstatus
Make it executable
$ chmod +x .git/hooks/pre-commit
You're good to go, from now it'd run rubocop against your changes and prevents from commiting unless you fix the offences.
How to skip pre-commit? just pass -n to git commit
$ git commit -n -m "[hotfix] blah blah"
From us to your inbox weekly.