Last active
August 29, 2025 09:54
-
-
Save iTrooz/551f779af2096912baa9d2e395e2642a to your computer and use it in GitHub Desktop.
Setup global git hooks that forward to local hooks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# This script sets up global git hooks (for global actions), that by default forward to local repository hooks if they exist. | |
# You can then edit these global hooks (make sure to keep the forwarding logic !) | |
HOOKS="applypatch-msg fsmonitor-watchman prepare-commit-msg pre-receive update post-update pre-commit pre-push push-to-checkout commit-msg pre-applypatch pre-merge-commit pre-rebase sendemail-validate" | |
HOOKS_DIR="$HOME/.config/git/hooks" | |
mkdir -p "$HOOKS_DIR" | |
git config --global core.hooksPath "$HOOKS_DIR" | |
for hook in $HOOKS; do | |
if [ -f "$HOOKS_DIR/$hook" ]; then | |
echo "Warning: $HOOKS_DIR/$hook already exists, skipping..." | |
continue | |
fi | |
DATA='''#!/bin/sh | |
# Keep this snippet after your global hook. It will have no effect (but not crash) if set in local hooks | |
LOCAL_HOOK=$(realpath ./.git/hooks/$(basename "$0")) | |
if [ -x "$LOCAL_HOOK" ] && [ "$LOCAL_HOOK" != "$(realpath $0)" ]; then | |
"$LOCAL_HOOK" "$@" | |
fi | |
''' | |
echo "$DATA" > "$HOOKS_DIR/$hook" | |
chmod +x "$HOOKS_DIR/$hook" | |
done | |
echo "Global git hooks have been set up in $HOOKS_DIR" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment