I have a project in C++ on Gitlab that is shared between several colleagues. We are discussing about to comment properly the commit and for this reason we would introduce a "Commit Policy", such that each commit is commented in a common way independently from the developer that did it. Please provide best practices or best policy or guideline to accomplish this task.
Assistant
Creating a "Commit Policy" for your C++ project on GitLab is a great idea to ensure consistency, clarity, and maintainability across your team's work. Here are some best practices and guidelines to help you establish an effective commit policy:
1. Commit Message Structure
Use a consistent structure for your commit messages. A common format includes:
Header (Short summary in 50 characters or less)
Body (Detailed description, wraps at 72 characters)
Footer (Optional, for issue references or breaking changes)
Example:
code
Header: Short summary of the changes
Body:
A detailed description of the changes being made. This can
span multiple lines and should provide enough context for
someone reviewing the commit to understand what was done and why.
Footer:
References to relevant issues, if any. E.g., "Closes #123"
2. Write Meaningful Headers
Be concise but descriptive: The header should clearly convey what the commit does without being too long.
Use imperative mood: This is a conventional style for commit messages, e.g., "Add new feature" instead of "Added new feature".
3. Provide Detailed Descriptions
Explain the Why: The body should explain why the changes were made, not just what was changed.
Break it down: If the changes are complex, break the description into paragraphs or bullet points for clarity.
4. Refer to Issues and Tasks
Reference issues: Use keywords like "Closes", "Fixes", or "Resolves" followed by the issue number. This links the commit to issue tracking.
Example: "Fixes #42 - Correct the calculation error in the algorithm"
5. Use Conventional Commits (Optional)
Consider adopting the "Conventional Commits" specification, which enforces a structured commit format. This can be particularly useful if using semantic versioning.
Feature branches: Use clear and consistent naming, e.g., feature/add-login
Bugfix branches: Follow similar logic, e.g., bugfix/fix-login-error
7. Example Commit Messages
Feature Addition:
code
feat(auth): add user login functionality
Implemented user login functionality using JWT tokens.
This allows users to authenticate and protects private routes.
Bug Fix:
code
fix(algorithm): resolve division by zero error
Added checks to prevent division by zero in the calculate function.
Fixes #123.
Documentation Update:
code
docs: update README with setup instructions
Added detailed setup instructions to the README file to help
new developers get started quickly.
8. Review and Enforce Commit Messages
Code Reviews: Make sure that the commit messages are reviewed during code reviews.
Automated Checks: Consider automating the enforcement of the commit message structure using tools like commitlint.
By adopting these guidelines, you can make your commit history more readable and easier to understand, facilitate better collaboration, and improve the overall quality of your project documentation.
9. Communicate and Document the Policy
Team Agreement: Ensure all team members are on board with the policy.
Document the Policy: Clearly document the commit policy in your project's repository (e.g., in a CONTRIBUTING.md file).
By following these guidelines, you'll help make sure that your project's commit history is informative, organized, and useful for everyone on your team.