Conventional commits spec
Overview
Conventional Commits is a strict set of rules for writing git commit messages. It forces you to write messages that both humans and machines can understand easily. ```python # Without conventional commits: msg_bad = 'fixed the thing' # With conventional commits: msg_good = 'fix: resolve login crash' ``` The structure is always `<type>: <description>`. The `type` tells you what kind of change it is. Common types are `feat` (new feature), `fix` (bug fix), and `docs` (documentation updates). ```python # Example of checking if a message is valid def is_valid(msg): # A simple check for the required format return msg.startswith('feat: ') or msg.startswith('fix: ') print(is_valid('feat: add user profile')) # True print(is_valid('added profile')) # False ``` The golden rule: **Always start your message with a valid type, followed by a colon and a space.** Think of it like putting a label on a tiffin box so you know exactly what is inside without opening it.
It makes the project history clean and readable. Automated tools can use these messages to generate release notes automatically.
Where used: Open source projects, Automated release pipelines
Why learn this
- It makes your commit history look professional.
- Many companies enforce this rule before allowing you to merge code.
Code walkthrough
commit_msg = 'feat: add payment gateway'
parts = commit_msg.split(': ')
commit_type = parts[0]
commit_desc = parts[1]
print(commit_type)
Focus: parts = commit_msg.split(': ')
Common mistakes
- Missing the space: Writing `feat:add login` instead of `feat: add login`. The space after the colon is strictly required.
- Using made-up types: Using a type like `update: changed button` instead of a standard type like `feat` or `fix`. Tools will fail to read it.
Recall questions
- Explain conventional commits in your own words, as if to a classmate.
- What is the correct structure of a conventional commit message?
- Why is it useful to follow this strict format?
Understanding checks
What does this print?
False
The space after the colon is missing in the input string. The rule strictly requires a space after the colon.
If you write a commit message `Fix: solve the crash`, will automated tools accept it?
No, because the type should typically be lowercase `fix`, not `Fix`.
Conventional commit types are strictly lowercase. Case matters when machines read the text.
Practice tasks
Fix the commit message
Given this invalid commit message string, change it to be a valid conventional commit for a bug fix.