Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials Git Git Hooks
Git Beginner FREE

Git Hooks

Lesson 1 of 17 Beginner Interactive

Git hooks are scripts that run automatically on certain Git events (commit, push, etc).

Syntax

GIT
# .git/hooks/pre-commit
#!/bin/sh
npm test

# .git/hooks/commit-msg
#!/bin/sh
# Check commit message format
Git Hooks
BASH
# .git/hooks/pre-commit
#!/bin/sh
# Run linting before commit
npm run lint
if [ $? -ne 0 ]; then
    echo "Linting failed. Commit aborted."
    exit 1
fi

# Using husky (Node.js)
# npm install husky --save-dev
# npx husky install
# npx husky add .husky/pre-commit "npm run lint"

Practice

1
Exercise

Practice this concept.

Answer
Run the commands as shown above.

Quick Quiz

1

What did you learn?

This covers the basics.

Interview Questions

It is a fundamental Git feature.