</>
Skip to content
Git lessons (36/44)

Git — Hooks

What are Hooks?

Scripts that run automatically on Git events.

Hook Locations

.git/hooks/
├── pre-commit
├── commit-msg
├── pre-push
└── post-merge

Common Hooks

HookWhen It Runs
pre-commitBefore commit
commit-msgAfter commit message
pre-pushBefore push
post-mergeAfter merge

Pre-Commit Hook

#!/bin/sh
# .git/hooks/pre-commit

# Run linter
npm run lint

# Run tests
npm test

Commit Message Hook

#!/bin/sh
# .git/hooks/commit-msg

# Check message length
if [ $(cat $1 | wc -l) -lt 10 ]; then
    echo "Commit message too short"
    exit 1
fi

Make Hook Executable

chmod +x .git/hooks/pre-commit

Disable Hook

# Temporarily skip hooks
git commit --no-verify -m "Skip hooks"

Mini Practice

  1. Create a pre-commit hook
  2. Add validation
  3. Make hook executable
  4. Test the hook

Up Next

Continue with GitHub — GitHub platform.

Related Topics

Frequently Asked Questions about Hooks

What is Hooks in Git?

Hooks is a fundamental concept in Git. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Hooks?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Hooks.

Why is Hooks important in Git?

Hooks is essential for Git development. Understanding this concept will help you write better code and solve real-world problems more effectively.