← Back to Git Course | Chapter 6: Advanced Commands | Lesson 2 of 7

Git Hooks

Git hooks doorbells जैसे हैं जो कुछ होने पर एक छोटा helper चलाते हैं, जैसे इसे turn in करने से ठीक पहले आपका homework check करना।
Syntax
bash
#!/bin/sh
# .git/hooks/hook_name
commands
# then: chmod +x .git/hooks/hook_name

Git Hooks Directory ढूँढना

Git hooks shell scripts हैं जिन्हें Git अपने workflow में specific points पर automatically चलाता है — एक commit से पहले, एक merge के बाद, एक push से पहले, वगैरह। वे किसी repository की .git/hooks/ directory में plain executable files (जैसे pre-commit) की तरह रहते हैं, और Git वहाँ किसी भी ऐसी file को ignore कर देता है जिसका नाम exactly सही न हो या जो executable न हो।

उदाहरण: Locating Git Hooks Directory

bash
ls .git/hooks/

एक Pre-commit Hook Set Up करना

एक pre-commit hook Git के commit message editor खोलने से ठीक पहले चलता है, और अगर script एक non-zero status के साथ exit हो, commit abort हो जाता है — इसे एक linter enforce करने, एक quick test suite चलाने, या debug statements वाली commits block करने की natural जगह बनाते हुए।

उदाहरण: Setting Up a Pre-commit Hook

bash
echo '#!/bin/sh\nnpm run lint' > .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit

Active Hooks Bypass करना

git commit --no-verify (या -n) उस एक commit के लिए pre-commit और commit-msg hooks दोनों skip करता है, आपको एक genuine emergency में आगे बढ़ने देते हुए — लेकिन routinely इस पर भरोसा करना hook होने का purpose ही खत्म कर देता है।

उदाहरण: Bypassing Active Hooks

bash
git commit --no-verify -m "Emergency fix"

Global Hooks Configure करना

क्योंकि .git/hooks/ Git द्वारा tracked नहीं है और repo के साथ clone नहीं होता, आपके लिखे hooks default रूप से local-only हैं। core.hooksPath को एक shared, version-controlled directory पर set करना (git config core.hooksPath .githooks के through) है कि teams हर किसी को same hooks कैसे distribute करती हैं।

उदाहरण: Configuring Global Hooks

bash
git config core.hooksPath .githooks

Post-merge Hooks बनाना

एक post-merge hook git merge या git pull complete होने के ठीक बाद fire होता है, जो इसे dependencies automatically reinstall करने या merge ने एक lockfile बदला हो तो एक stale build cache clear करने के लिए एक अच्छी जगह बनाता है — एक out-of-date environment की वजह से होने वाले "works on my machine" bugs की एक class पकड़ते हुए।

उदाहरण: Creating Post-merge Hooks

bash
echo '#!/bin/sh\nnpm install' > .git/hooks/post-merge
chmod +x .git/hooks/post-merge
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. hook बनाना लेकिन chmod +x भूल जाना, ताकि Git इसे चुपचाप ignore कर दे।
  2. hook को गलत नाम देना, जैसे pre-commit.sh, जबकि file का नाम exactly pre-commit होना चाहिए।
  3. .git/hooks में hooks के team के साथ shared होने की उम्मीद करना, जबकि वे commit नहीं होते और हर किसी को उन्हें install करना ज़रूरी है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.