A set of shell scripts to automatically detect and prevent Chinese characters in Git commits using global pre-commit hooks.
- 🔍 Automatic Detection: Detects Chinese characters in staged files before commit
- 🌍 Cross-Platform: Works on both macOS (with ggrep) and Linux (with grep)
- 📝 Configurable Ignore: Support for ignore patterns to skip specific files/directories
- ⚡ Easy Installation: One-command setup with automatic validation
- 🧪 Self-Testing: Built-in tests to verify hook functionality
# Clone the repository
git clone https://github.com/alongLFB/git_hook_for_self.git
cd git_hook_for_self
# Make scripts executable
chmod +x install-global-precommit.sh check-global-hook.sh
# Install the global pre-commit hook
./install-global-precommit.shIf you're on macOS, install GNU grep for better Unicode support:
brew install grepCheck if the hook is working correctly:
./check-global-hook.sh- Global Hook: Sets up a global Git pre-commit hook that applies to all repositories
- Chinese Detection: Uses regex pattern
[\x{4e00}-\x{9fff}]to detect Chinese characters - File Filtering: Only checks text files and respects ignore patterns
- Commit Prevention: Blocks commits containing Chinese characters with detailed error messages
Edit ~/.githooks/ignore-chinese.txt to customize which files to skip:
# Ignore patterns for Chinese detection
README_CN.md
*.md
docs/*
install-global-precommit.sh
check-global-hook.shSupported patterns:
- Exact filenames:
README_CN.md - Wildcards:
*.md,docs/* - Directory patterns:
docs/*
- Hook file:
~/.githooks/pre-commit - Ignore file:
~/.githooks/ignore-chinese.txt - Git config:
core.hooksPath = ~/.githooks
echo "Hello World" > test.txt
git add test.txt
git commit -m "Add test file"
# ✅ Commit succeedsecho "你好世界" > test.txt
git add test.txt
git commit -m "Add test file"
# ❌ Chinese characters detected in file: test.txt
# 1:你好世界
# ⚠️ Commit rejected. Please remove Chinese characters before committing.echo "你好世界" > README_CN.md
git add README_CN.md
git commit -m "Add Chinese README"
# ✅ Skipping ignored file: README_CN.md
# ✅ Commit succeeds- Check if
core.hooksPathis set:git config --get core.hooksPath - Verify hook exists and is executable:
ls -la ~/.githooks/pre-commit - Run validation script:
./check-global-hook.sh
- Ensure ggrep is installed on macOS:
brew install grep - Test regex manually:
ggrep -P "[\x{4e00}-\x{9fff}]" yourfile.txt - Check file encoding (UTF-8 recommended)
chmod +x ~/.githooks/pre-commit-
install-global-precommit.sh: Main installation script- Sets up global hooks directory
- Configures Git to use global hooks
- Creates ignore patterns file
- Installs and tests the pre-commit hook
-
check-global-hook.sh: Validation and troubleshooting script- Verifies hook installation
- Checks prerequisites (ggrep on macOS)
- Tests hook functionality
- Fixes common issues automatically
- Git 2.9+ (for global hooks support)
- Bash shell
- grep with Perl regex support:
- macOS:
ggrep(install viabrew install grep) - Linux:
grep(usually pre-installed)
- macOS:
MIT License - feel free to use and modify as needed.
- Fork the repository
- Create your feature branch
- Make your changes
- Test thoroughly
- Submit a pull request