Mid From PDF Version Control Git & GitHub

What are hooks in Git? Can you give examples?

Git hooks are scripts that run automatically when specific Git events occur (like committing

or pushing).

They live inside .git/hooks/.

Common examples:

pre-commit: Run linting or unit tests before a commit

# .git/hooks/pre-commit

npm run lint || exit 1

  • pre-push: Prevent pushes to main
if [ "$(git rev-parse --abbrev-ref HEAD)" == "main" ]; then

echo "You can't push directly to main!"

exit 1

fi

  • Example:

In a team, we set a pre-commit hook to check code formatting with ESLint before any

commit — ensuring consistency across all contributors.

More from Developer Essentials

All questions for this course
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details