Git Commit Messages: A Template for Conventional Practices
Clear and structured commit messages are vital for maintaining a project's integrity, enhancing collaboration among team members, and providing a useful history of changes. Below is a template for writing conventional Git commit messages and examples for various categories.
Feature Additions
feat: add user authentication module
feat: implement API for product listing
feat: create responsive navbar component
Fixes
fix: correct error handling in login component
fix: resolve bug causing page crash on load
fix: update regex to validate email format
Documentation
docs: update README with setup instructions
docs: add API documentation for endpoints
docs: clarify installation steps in CONTRIBUTING.md
Styling
style: reformat code with consistent spacing
style: remove extra whitespace in CSS files
style: adjust button padding for visual consistency
Refactoring
refactor: modularize user service functions
refactor: extract database queries to separate file
refactor: simplify component structure in header
Performance Improvements
perf: implement caching for frequently accessed data
perf: optimize image loading with lazy loading
perf: reduce API call frequency on the main page
Testing
test: add unit tests for user registration
test: update snapshot tests for navbar component
test: add integration tests for search functionality
Chores
chore: update npm dependencies to latest versions
chore: configure ESLint rules for consistent syntax
Tips for Writing Effective Commit Messages
- Be Concise but Descriptive: Aim for a balance between brevity and clarity. A good message should quickly convey the purpose of the commit.
- Example:
fix: correct error handling in login component
- Example:
- Use the Imperative Mood: Write messages in the imperative form (e.g., "add," "fix") to provide clear instructions about what the commit does.
- Example:
feat: add user profile page
- Example:
- Reference Issues or Tasks: If applicable, include references to issue numbers or tasks (e.g.,
Fixes #123
) to provide additional context and traceability.- Example:
fix: resolve bug causing page crash on load (Fixes #45)
- Example:
- Use Brackets to Specify the Type: Include brackets around the type of change at the beginning of your message (e.g.,
[feat]
,[fix]
) to make it instantly clear what kind of change is being made.- Example:
[style] adjust button padding for visual consistency
- Example:
Examples of Poor vs. Improved Commit Messages
Here are some common poor commit messages and their improved versions:
-
Poor:
fix stuff
- Improved:
fix: resolve bug causing incorrect data display on user profile
- Improved:
-
Poor:
update
- Improved:
docs: update README to include new installation instructions
- Improved:
-
Poor:
made changes
- Improved:
style: reformat CSS for consistency across all components
- Improved:
By sticking to a structured approach for your commit messages, you’re not just making your life easier—you’re boosting collaboration and keeping your project history crystal clear! Embracing these conventions means your team can quickly grasp changes and level up your code management game. So let’s get those commit messages on point and watch your project thrive!