Skip to content

Git Hook ‐ Prepare commit msg

maple edited this page Dec 23, 2024 · 2 revisions

🪝 Semantic Emoji Git Hook

A Git commit-msg hook that automatically adds semantic emojis to your commit messages based on conventional commit types.

✨ Features

  • Automatically prepends appropriate emojis to your commits
  • Supports conventional commit types
  • Handles optional scopes in commit messages
  • Preserves existing emoji commits
  • Follows gitmoji standards

📥 Installation

  1. Save the script as .git/hooks/commit-msg:
curl -o .git/hooks/commit-msg https://raw.githubusercontent.com/maplepy/dotfiles/master/dot_git-templates/hooks/executable_prepare-commit-msg
chmod +x .git/hooks/commit-msg
  1. Make it executable:
chmod +x .git/hooks/commit-msg

🎯 Usage

Simply write your commits following the conventional commit format:

# Basic commit
git commit -m "feat: add new feature"
# ➡️ "✨ feat: add new feature"

# Commit with scope
git commit -m "fix(auth): resolve login issue"
# ➡️ "🐛 fix(auth): resolve login issue"

🎯 Commit Types Reference

Primary Types

Type Emoji Purpose Examples
init 🎉 Project initialization init: bootstrap project
init(v2): start major version
init(monorepo): setup workspace
feat New features feat: add user authentication
feat(dashboard): add data visualization
feat(api): implement endpoints
fix 🐛 Bug fixes fix: resolve memory leak
fix(auth): fix token expiration
fix(ui): correct alignment
docs 📝 📚 Documentation docs: update API docs
docs(readme): add deployment guide
docs(api): update specs
style 🎨 Code style/formatting style: apply prettier formatting
style(css): standardize colors
style(lint): fix warnings

Technical Changes

Type Emoji Purpose Examples
refactor ♻️ Code restructuring refactor: convert to TypeScript
refactor(auth): switch to JWT
refactor(api): improve structure
perf ⚡️ Performance improvements perf: optimize loading
perf(queries): add indexes
perf(api): implement caching
test ✅ 🧪 Testing changes test: add unit tests
test(api): add integration tests
test(e2e): add flow tests

Infrastructure

Type Emoji Purpose Examples
build 👷 Build system build: upgrade webpack
build(deps): update packages
build(docker): optimize image
ci 💚 CI/CD changes ci: add deployment
ci(github): setup actions
ci(tests): add coverage
chore 🔧 Maintenance tasks chore: update dependencies
chore(git): update ignore
chore(deps): bump versions

Additional Types

Type Emoji Purpose Examples
deps 📦 Dependencies deps: upgrade react
deps(security): patch packages
deps(types): update definitions
security 🔒 Security updates security: add XSS protection
security(auth): add rate limiting
security(api): add validation
revert ⏪️ Revert changes revert: remove feature
revert(auth): restore sessions
revert(ui): restore design
wip 🚧 Work in progress wip: start oauth
wip(ui): draft layout
wip(api): begin migration
deploy 🚀 Deployment deploy: release v1.0
deploy(staging): update env
deploy(prod): migrate db

🎯 Scope Examples

Scopes provide additional context in parentheses:

feat(ui): add new button component
fix(auth): resolve token refresh
perf(api): optimize database queries
docs(readme): update installation steps

Common scope categories:

  • Components: (ui), (api), (db)
  • Features: (auth), (user), (admin)
  • Technical: (core), (deps), (test)
  • Platform: (ios), (android), (web)

🔄 Behavior

  1. If a commit already has an emoji, it's left unchanged
  2. Commits without a valid prefix default to "feat"
  3. Scopes are optional but preserved if present
  4. The hook maintains the original message case

💡 Tips

  • Keep commit messages concise and descriptive
  • Use scopes to indicate affected components
  • Follow the conventional commits specification
  • One change per commit for better history

🛠️ Customization

To add or modify emoji mappings, edit the prefix_emoji array in the script:

declare -A prefix_emoji=(
    ["your-type"]="🔥"  # Add custom types
)

🤝 Contributing

Feel free to submit issues and enhancement requests!

  1. Fork the repository
  2. Create your feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a Pull Request