Git Hooks
In this page:
#!/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
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
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
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
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
echo '#!/bin/sh\nnpm install' > .git/hooks/post-merge
chmod +x .git/hooks/post-merge
- hook बनाना लेकिन
chmod +xभूल जाना, ताकि Git इसे चुपचाप ignore कर दे। - hook को गलत नाम देना, जैसे
pre-commit.sh, जबकि file का नाम exactlypre-commitहोना चाहिए। .git/hooksमें hooks के team के साथ shared होने की उम्मीद करना, जबकि वे commit नहीं होते और हर किसी को उन्हें install करना ज़रूरी है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: