Debian Codegraph @

docs/tutorials/writing_commits.md

raw
1 # Commit Message Best Practices
2
3 ## About
4
5 For Commitizen to work effectively in your development pipeline, commits must follow a consistent convention. By default, Commitizen uses the [Conventional Commits][conventional_commits] specification, which provides a standardized format for commit messages that enables automatic versioning and changelog generation.
6
7 You can also create your own custom commit convention by following the [customization documentation][customization].
8
9 ## Conventional Commits Format
10
11 The Conventional Commits specification follows this structure:
12
13 ```
14 <type>[optional scope]: <description>
15
16 [optional body]
17
18 [optional footer]
19 ```
20
21 ### Commit Types
22
23 Commit types categorize the nature of your changes. The most important types for semantic versioning are:
24
25 - **`feat`**: Introduces a new feature (correlates with **MINOR** version increment)
26 - **`fix`**: Patches a bug (correlates with **PATCH** version increment)
27
28 Other commonly used types include:
29
30 - **`docs`**: Documentation only changes
31 - **`style`**: Code style changes (formatting, missing semicolons, etc.)
32 - **`refactor`**: Code refactoring without changing functionality
33 - **`perf`**: Performance improvements
34 - **`test`**: Adding or updating tests
35 - **`build`**: Changes to build system or dependencies
36 - **`ci`**: Changes to CI configuration files
37 - **`chore`**: Other changes that don't modify source or test files
38
39 !!! note
40 While `feat` and `fix` directly affect semantic versioning, other types (like `build`, `chore`, `docs`) typically don't trigger version bumps unless they include a `BREAKING CHANGE`.
41
42 ### Breaking Changes
43
44 Breaking changes trigger a **MAJOR** version increment. You can indicate breaking changes in two ways:
45
46 1. **In the commit body or footer**:
47 ```
48 feat(api): change authentication method
49
50 BREAKING CHANGE: The authentication API now requires OAuth2 instead of API keys.
51 ```
52
53 2. **In the commit title** (when enabled):
54 ```
55 feat!: change authentication method
56 ```
57
58 To enable this syntax, set `breaking_change_exclamation_in_title = true` in your configuration. [Read more][breaking-change-config]
59
60 ### Scope
61
62 An optional scope can be added to provide additional context about the area of the codebase affected:
63
64 ```
65 feat(parser): add support for JSON arrays
66 fix(api): handle null response gracefully
67 ```
68
69 ## Writing Effective Commit Messages
70
71 Well-written commit messages are crucial for maintaining a clear project history. When writing commits, consider:
72
73 - **Your future self**: You'll need to understand these changes months later
74 - **Your team**: Clear messages help colleagues understand the codebase evolution
75 - **Automated tools**: Good messages enable better changelog generation and versioning
76
77 ### Best Practices
78
79 #### 1. Keep the Subject Line Concise
80
81 The subject line should be clear and concise (aim for ~50 characters). It should summarize what the commit does in one line.
82
83 **Good:**
84 ```
85 fix(commands): handle missing user input gracefully
86 feat(api): add pagination support
87 ```
88
89 **Avoid:**
90 ```
91 fix: stuff
92 feat: commit command introduced
93 ```
94
95 #### 2. Use Imperative Mood
96
97 Write commit messages in the imperative mood, as if completing the sentence: "If applied, this commit will..."
98
99 **Good:**
100 ```
101 feat: add user authentication
102 fix: resolve memory leak in parser
103 ```
104
105 **Avoid:**
106 ```
107 feat: added user authentication
108 fix: resolved memory leak in parser
109 ```
110
111 #### 3. Think About the Changelog
112
113 Your commits will likely appear in the automatically generated changelog. Write messages that make sense in that context. If you want to exclude a commit from the changelog, use types like `build`, `chore`, or `ci`.
114
115 #### 4. One Feature Per Commit
116
117 Keep commits focused on a single change. If you introduce multiple related changes, consider squashing them into a single commit. This makes the history cleaner and improves changelog generation.
118
119 #### 5. Use the Body for Context
120
121 For complex changes, use the commit body to explain:
122
123 - **Why** the change was made
124 - **What** was changed (if not obvious from the subject)
125 - **How** it differs from previous behavior
126
127 ```
128 feat(api): add rate limiting
129
130 Implement rate limiting to prevent API abuse. The system now
131 enforces a maximum of 100 requests per minute per IP address.
132 Exceeding this limit returns a 429 status code.
133 ```
134
135 ### Examples
136
137 | ✅ Good Examples | ❌ Poor Examples |
138 | ---------------- | ---------------- |
139 | `fix(commands): bump error when no user provided` | `fix: stuff` |
140 | `feat(api): add pagination to user list endpoint` | `feat: commit command introduced` |
141 | `docs: update installation instructions` | `docs: changes` |
142 | `refactor(parser): simplify token extraction logic` | `refactor: code cleanup` |
143
144 ## Character Encoding
145
146 Commitizen supports Unicode characters (including emojis) in commit messages. This is useful if you're using commit message formats that include emojis, such as [cz-emoji][cz_emoji].
147
148 By default, Commitizen uses `utf-8` encoding. You can configure a different encoding through the `encoding` [configuration option][configuration].
149
150 ## Related Documentation
151
152 - [Conventional Commits Specification][conventional_commits]
153 - [Custom Commit Conventions][customization]
154 - [Commit Configuration Options](../config/commit.md)
155
156 [customization]: ../customization/config_file.md
157 [conventional_commits]: https://www.conventionalcommits.org
158 [cz_emoji]: ../third-party-plugins/cz-emoji.md
159 [configuration]: ../config/commit.md#encoding
160 [breaking-change-config]: ../config/commit.md#breaking_change_exclamation_in_title