Run tasks in a Git pre-commit hook
23 April 2022 (Updated 24 April 2022)
Suppose you’re working on a Rails app and you want to run your RSpec tests before a Git commit is allowed. Here’s a possible approach.
Create executable pre-commit hook file
touch .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
Add hooks to file
exec ./bin/rubocop
exec ./bin/rspec spec/
Verify hook is running
Make a small change (e.g., add a new line to your README.md
) and then commit as usual:
git commit
Your hooks (e.g., ./bin/rspec spec/
) should run. If any of them returns with a non-zero status code, the commit will be rejected.
Tagged:
Git
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment