Commit message best practices
- Commit Message Best Practices
This article details the best practices for writing commit messages when using a version control system like Git, as commonly integrated with MediaWiki. Clear and informative commit messages are crucial for maintaining a healthy and understandable project history, facilitating collaboration, and simplifying debugging. These principles apply whether you’re contributing to the core MediaWiki codebase, an extension, or a skin.
Why are Good Commit Messages Important?
Consider your commit history as a project's memory. Future developers (including your future self!) will rely on it to understand *why* changes were made, not just *what* changes were made. Poorly written commit messages can lead to:
- **Difficulty understanding the codebase:** Trying to decipher the intent behind changes without a clear message is time-consuming and frustrating.
- **Debugging nightmares:** When tracking down the source of a bug, commit messages can provide valuable clues about when and why a problematic change was introduced.
- **Collaboration challenges:** A shared understanding of the project's evolution is essential for effective teamwork. Vague messages hinder this understanding.
- **Reduced code review quality:** Reviewers need context to effectively assess changes. Good commit messages provide that context.
- **Automated Tooling Issues:** Many automated tools (release notes generation, changelog creation, blame, bisect) rely on well-formatted commit messages to function correctly.
The Anatomy of a Good Commit Message
A good commit message typically consists of the following parts:
1. **Subject Line:** A concise summary of the change (maximum 50 characters). 2. **Blank Line:** Separates the subject line from the body. *Crucially important*. 3. **Body:** A more detailed explanation of *why* the change was made, *what* the change does, and any potential side effects. This can span multiple paragraphs. 4. **Footer (Optional):** Information about related issues, breaking changes, or co-authors.
Let's break down each part with examples tailored to a MediaWiki context.
1. The Subject Line
The subject line is the most important part of the commit message. It should be:
- **Concise:** Aim for 50 characters or less. This ensures it displays well in various Git tools.
- **Imperative Mood:** Use the imperative mood ("Fix bug," not "Fixed bug" or "Fixes bug"). Think of it as completing a sentence that starts with "If applied, this commit will…". For example: "Add unit test for ParserHook" rather than "Added unit test for ParserHook."
- **Specific:** Avoid vague terms like "Update," "Fix," or "Changes." Instead, describe *what* is being updated or fixed.
- **Capitalized:** Start with a capital letter.
- **No Period:** Do not end the subject line with a period.
- Examples:**
- **Good:** "Refactor: Improve performance of category listing"
- **Good:** "Fix: Prevent XSS vulnerability in user profile"
- **Good:** "Add: New skin option for customizing sidebar"
- **Bad:** "Updated files"
- **Bad:** "Fixed a bug"
- **Bad:** "Changes to the database"
2. The Blank Line
This seemingly insignificant line is vital. It separates the subject line from the body, allowing Git tools to correctly parse the message. Without it, the entire message may be treated as the subject line. This is a common mistake!
3. The Body
The body provides more context and detail about the commit. It should answer the following questions:
- **Why was this change made?** What problem does it solve? What motivated the change? Referencing a Bugzilla ticket number is highly recommended.
- **What does this change do?** Describe the specific changes made and how they address the problem.
- **Are there any side effects?** Are there any potential drawbacks or compatibility issues?
- **How was this tested?** Describe the testing process used to ensure the change is correct.
The body should be written in complete sentences and paragraphs, using proper grammar and spelling. Wrap lines at 72 characters for readability in various environments.
- Example:**
``` Fix: Prevent XSS vulnerability in user profile
This commit addresses a potential cross-site scripting (XSS) vulnerability in the user profile page. Specifically, the user's "About me" field was not properly sanitized, allowing malicious JavaScript code to be injected and executed in other users' browsers.
The fix involves encoding the user's "About me" field using htmlspecialchars() before it is displayed on the page. This ensures that any potentially harmful characters are escaped.
This change has been tested by:
- Manually entering malicious JavaScript code into the "About me"
field and verifying that it is not executed.
- Running automated security scans using [OWASP ZAP](https://www.zaproxy.org/).
Related: Bugzilla ticket #12345 ```
The footer can be used to provide additional information, such as:
- **References to Issue Trackers:** "Related: Bugzilla ticket #12345" or "Refs: #12345"
- **Breaking Changes:** "BREAKING CHANGE: This commit introduces a breaking
change to the API. Users will need to update their code to be compatible."
- **Co-authors:** "Co-authored-by: John Doe <[email protected]>" (useful for collaborative work)
- **Cross-references to other commits:** "See commit abcdefg for related changes."
- Example:**
``` Refactor: Improve performance of category listing
This commit refactors the code responsible for generating category listings to improve performance. The previous implementation was inefficient due to repeated database queries.
The new implementation caches the category data in memory, reducing the number of database queries required. This results in a significant performance improvement, especially for categories with a large number of articles.
This change has been tested by:
- Measuring the page load time for category pages before and after
the change.
- Running performance tests using [Apache JMeter](https://jmeter.apache.org/).
BREAKING CHANGE: The category listing API has been changed. Users who rely on the previous API will need to update their code.
Refs: Bugzilla ticket #56789 ```
Common Mistakes to Avoid
- **Vague Subject Lines:** "Fix bug," "Update code," "Changes."
- **Missing Blank Line:** The space between the subject and body is *required*.
- **Long Subject Lines:** Keep them under 50 characters.
- **Non-Imperative Mood:** "Fixed," "Fixes," "Updating."
- **Lack of Context:** Failing to explain *why* the change was made.
- **Ignoring Line Length:** Wrap lines at 72 characters for readability.
- **Committing Work-in-Progress:** Commit only complete, logical units of change. Use staging area effectively. Avoid committing "fix typo" or "minor changes" without context.
- **Committing Large Changes:** Break down large changes into smaller, more manageable commits.
Tools to Help
- **Git Hooks:** You can configure Git hooks to automatically check commit messages for compliance with your team's standards. See resources like [Husky](https://typicode.github.io/husky/) and [Commitlint](https://github.com/conventional-changelog/commitlint).
- **Linters:** Tools like [commitlint](https://github.com/conventional-changelog/commitlint) can be integrated into your CI/CD pipeline to enforce commit message standards.
- **Editor Integration:** Many IDEs and text editors have plugins that can help you write better commit messages.
Commit Message Conventions
While the above guidelines are generally accepted, specific projects may have their own conventions. It's important to familiarize yourself with the conventions used in the project you're contributing to. Common conventions include:
- **Conventional Commits:** A specification for creating standardized commit messages. See [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/). This is gaining popularity.
- **Angular Commit Message Guidelines:** A similar set of guidelines used by the Angular project. See [Angular Commit Message Guidelines](https://github.com/angular/angular/blob/master/CONTRIBUTING.md#commit).
- **Project-Specific Conventions:** MediaWiki itself may have specific requirements documented in its Contributing to MediaWiki guidelines.
Further Resources
- [How to Write a Git Commit Message](https://chris.beams.io/posts/git-commit/)
- [A Note About Git Commit Messages](https://www.atlassian.com/git/tutorials/writing-good-commit-messages)
- [Git Commit Message Style Guide](https://github.com/joelparker/git-style-guide)
- [Conventional Commits Specification](https://www.conventionalcommits.org/en/v1.0.0/)
- [OWASP ZAP](https://www.zaproxy.org/) - Web application security scanner
- [Apache JMeter](https://jmeter.apache.org/) - Performance testing tool
- [Bugzilla](https://www.bugzilla.org/) - Issue tracking system
- [htmlspecialchars()](https://www.php.net/manual/en/function.htmlspecialchars.php) – PHP function for escaping HTML entities
- MediaWiki developer documentation
- MediaWiki coding conventions
- Extending MediaWiki
- Skin development
- [Trading Psychology](https://www.investopedia.com/terms/t/trading-psychology.asp)
- [Technical Analysis](https://www.investopedia.com/terms/t/technicalanalysis.asp)
- [Candlestick Patterns](https://www.investopedia.com/terms/c/candlestick.asp)
- [Moving Averages](https://www.investopedia.com/terms/m/movingaverage.asp)
- [Relative Strength Index (RSI)](https://www.investopedia.com/terms/r/rsi.asp)
- [MACD](https://www.investopedia.com/terms/m/macd.asp)
- [Bollinger Bands](https://www.investopedia.com/terms/b/bollingerbands.asp)
- [Fibonacci Retracements](https://www.investopedia.com/terms/f/fibonacciretracement.asp)
- [Elliott Wave Theory](https://www.investopedia.com/terms/e/elliottwavetheory.asp)
- [Support and Resistance Levels](https://www.investopedia.com/terms/s/supportandresistance.asp)
- [Trend Lines](https://www.investopedia.com/terms/t/trendline.asp)
- [Chart Patterns](https://www.investopedia.com/terms/c/chartpattern.asp)
- [Risk Management](https://www.investopedia.com/terms/r/riskmanagement.asp)
- [Position Sizing](https://www.investopedia.com/terms/p/position-sizing.asp)
- [Diversification](https://www.investopedia.com/terms/d/diversification.asp)
- [Correlation](https://www.investopedia.com/terms/c/correlation.asp)
- [Volatility](https://www.investopedia.com/terms/v/volatility.asp)
- [Market Sentiment](https://www.investopedia.com/terms/m/marketsentiment.asp)
- [Fundamental Analysis](https://www.investopedia.com/terms/f/fundamentalanalysis.asp)
Start Trading Now
Sign up at IQ Option (Minimum deposit $10) Open an account at Pocket Option (Minimum deposit $5)
Join Our Community
Subscribe to our Telegram channel @strategybin to receive: ✓ Daily trading signals ✓ Exclusive strategy analysis ✓ Market trend alerts ✓ Educational materials for beginners