|
| 1 | +--- |
| 2 | +description: Git commit guidelines to ensure consistent and error-free commits |
| 3 | +globs: |
| 4 | +--- |
| 5 | + |
| 6 | +# Git Commit Guidelines |
| 7 | + |
| 8 | +## Commit Message Format |
| 9 | + |
| 10 | +### Structure |
| 11 | + |
| 12 | +The commit message should be structured as follows: |
| 13 | + ``` |
| 14 | + <type>[optional scope]: <description> |
| 15 | + |
| 16 | + [optional body] |
| 17 | + |
| 18 | + [optional footer(s)] |
| 19 | + ``` |
| 20 | + |
| 21 | +### Types |
| 22 | + |
| 23 | +- `feat`: New feature |
| 24 | +- `fix`: Bug fix |
| 25 | +- `docs`: Documentation |
| 26 | +- `style`: Formatting |
| 27 | +- `refactor`: Code restructuring |
| 28 | +- `test`: Test changes |
| 29 | +- `chore`: Maintenance |
| 30 | + |
| 31 | +## Specification |
| 32 | + |
| 33 | +- Commits MUST be prefixed with a type, which consists of a noun, feat, fix, etc., followed by the OPTIONAL scope, OPTIONAL !, and REQUIRED terminal colon and space. |
| 34 | +- The type feat MUST be used when a commit adds a new feature to your application or library. |
| 35 | +- The type fix MUST be used when a commit represents a bug fix for your application. |
| 36 | +- A scope MAY be provided after a type. A scope MUST consist of a noun describing a section of the codebase surrounded by parenthesis, e.g., fix(parser): |
| 37 | +- A description MUST immediately follow the colon and space after the type/scope prefix. The description is a short summary of the code changes, e.g., fix: array parsing issue when multiple spaces were contained in string. |
| 38 | +- A longer commit body MAY be provided after the short description, providing additional contextual information about the code changes. The body MUST begin one blank line after the description. |
| 39 | +- A commit body is free-form and MAY consist of any number of newline separated paragraphs. |
| 40 | +- One or more footers MAY be provided one blank line after the body. Each footer MUST consist of a word token, followed by either a :<space> or <space># separator, followed by a string value (this is inspired by the git trailer convention). |
| 41 | +- A footer’s token MUST use - in place of whitespace characters, e.g., Acked-by (this helps differentiate the footer section from a multi-paragraph body). An exception is made for BREAKING CHANGE, which MAY also be used as a token. |
| 42 | +- A footer’s value MAY contain spaces and newlines, and parsing MUST terminate when the next valid footer token/separator pair is observed. |
| 43 | +- Breaking changes MUST be indicated in the type/scope prefix of a commit, or as an entry in the footer. |
| 44 | +- If included as a footer, a breaking change MUST consist of the uppercase text BREAKING CHANGE, followed by a colon, space, and description, e.g., BREAKING CHANGE: environment variables now take precedence over config files. |
| 45 | +- If included in the type/scope prefix, breaking changes MUST be indicated by a ! immediately before the :. If ! is used, BREAKING CHANGE: MAY be omitted from the footer section, and the commit description SHALL be used to describe the breaking change. |
| 46 | +- Types other than feat and fix MAY be used in your commit messages, e.g., docs: update ref docs. |
| 47 | +- The units of information that make up Conventional Commits MUST NOT be treated as case sensitive by implementors, with the exception of BREAKING CHANGE which MUST be uppercase. |
| 48 | +BREAKING-CHANGE MUST be synonymous with BREAKING CHANGE, when used as a token in a footer. |
| 49 | + |
| 50 | +## Error Prevention |
| 51 | + |
| 52 | +### ✅ CORRECT Patterns |
| 53 | + |
| 54 | +- Commit message with description and breaking change footer: |
| 55 | + ```bash |
| 56 | + git commit -m 'feat: allow provided config object to extend other configs' -m 'BREAKING CHANGE: `extends` key in config file is now used for extending other config files' |
| 57 | + ``` |
| 58 | + |
| 59 | +- Commit message with ! to draw attention to breaking change: |
| 60 | + ```bash |
| 61 | + git commit -m "feat!: send an email to the customer when a product is shipped" |
| 62 | + ``` |
| 63 | + |
| 64 | +- Commit message with scope and ! to draw attention to breaking change |
| 65 | + ```bash |
| 66 | + git commit -m "feat(api)!: send an email to the customer when a product is shipped" |
| 67 | + ``` |
| 68 | + |
| 69 | +- Commit message with both ! and BREAKING CHANGE footer: |
| 70 | + ```bash |
| 71 | + git commit -m "chore!: drop support for Node 6" -m "BREAKING CHANGE: use JavaScript features not available in Node 6." |
| 72 | + ``` |
| 73 | + |
| 74 | +- Commit message with no body: |
| 75 | + ```bash |
| 76 | + git commit -m "docs: correct spelling of CHANGELOG" |
| 77 | + ``` |
| 78 | + |
| 79 | +- Commit message with scope: |
| 80 | + ```bash |
| 81 | + git commit -m "feat(lang): add Polish language" |
| 82 | + ``` |
| 83 | + |
| 84 | +- Commit message with multi-paragraph body and multiple footers: |
| 85 | + ```bash |
| 86 | + git commit -m "fix: prevent racing of requests |
| 87 | + |
| 88 | + Introduce a request id and a reference to latest request. Dismiss |
| 89 | + incoming responses other than from latest request. |
| 90 | + |
| 91 | + Remove timeouts which were used to mitigate the racing issue but are |
| 92 | + obsolete now. |
| 93 | + |
| 94 | + Reviewed-by: Z |
| 95 | + Refs: #123" |
| 96 | + ``` |
| 97 | + |
| 98 | +### ❌ INCORRECT Patterns |
| 99 | + |
| 100 | + ```bash |
| 101 | + # No prefix used |
| 102 | + git commit -m "Add code" |
| 103 | + ``` |
| 104 | + |
| 105 | +## Best Practices |
| 106 | + |
| 107 | +### Message Guidelines |
| 108 | + |
| 109 | +1. Use one of the message types from the list of the provided above types |
| 110 | +2. Use imperative mood ("add" not "adds") |
| 111 | +3. Keep first line under 50 characters |
| 112 | +4. No period at end of subject line |
| 113 | +5. Be specific and clear |
| 114 | +6. Reference issue numbers when relevant |
| 115 | + |
| 116 | +### Commit Organization |
| 117 | + |
| 118 | +1. Make atomic commits (one logical change) |
| 119 | +2. Verify changes before committing |
| 120 | +3. Run tests before pushing |
| 121 | +4. Review the diff before committing |
| 122 | +5. Keep commits focused and small |
| 123 | + |
| 124 | +## Common Patterns |
| 125 | + |
| 126 | +### Feature Work |
| 127 | + |
| 128 | + ```bash |
| 129 | + git commit -m "feat(user): add email verification" |
| 130 | + git commit -m "feat(auth): implement 2FA support" |
| 131 | + ``` |
| 132 | + |
| 133 | +### Bug Fixes |
| 134 | + |
| 135 | + ```bash |
| 136 | + git commit -m "fix(api): resolve rate limiting issue" |
| 137 | + git commit -m "fix(ui): correct button alignment" |
| 138 | + ``` |
| 139 | + |
| 140 | +### Documentation |
| 141 | + |
| 142 | + ```bash |
| 143 | + git commit -m "docs(api): update endpoint documentation" |
| 144 | + git commit -m "docs(setup): add deployment guide" |
| 145 | + ``` |
| 146 | + |
| 147 | +### Refactoring |
| 148 | + |
| 149 | + ```bash |
| 150 | + git commit -m "refactor(core): optimize data processing" |
| 151 | + git commit -m "refactor(auth): simplify login flow" |
| 152 | + ``` |
| 153 | + |
| 154 | +## Recovery Procedures |
| 155 | + |
| 156 | +### Fix Last Commit |
| 157 | + |
| 158 | + ```bash |
| 159 | + # Amend commit message |
| 160 | + git commit --amend -m "type(scope): corrected message" |
| 161 | + |
| 162 | + # Amend commit content |
| 163 | + git add forgotten-file |
| 164 | + git commit --amend --no-edit |
| 165 | + ``` |
| 166 | + |
| 167 | +### Split Failed Commit |
| 168 | + |
| 169 | + ```bash |
| 170 | + # Reset last commit |
| 171 | + git reset HEAD~1 |
| 172 | + |
| 173 | + # Commit changes separately |
| 174 | + git add feature-files |
| 175 | + git commit -m "feat(scope): first part" |
| 176 | + git add test-files |
| 177 | + git commit -m "test(scope): add tests" |
| 178 | + ``` |
| 179 | + |
| 180 | +## Pre-commit Checklist |
| 181 | + |
| 182 | +### Code Quality |
| 183 | + |
| 184 | +- Run linters |
| 185 | +- Check formatting |
| 186 | +- Remove debug code |
| 187 | +- Run tests |
| 188 | + |
| 189 | +### Security |
| 190 | + |
| 191 | +- No sensitive data |
| 192 | +- No API keys |
| 193 | +- No credentials |
| 194 | +- Check file permissions |
| 195 | + |
| 196 | +### Commit Message |
| 197 | + |
| 198 | +- Correct type and scope |
| 199 | +- Clear description |
| 200 | +- Reference issues if applicable |
| 201 | + |
| 202 | +### Changes |
| 203 | + |
| 204 | +- Review diff |
| 205 | +- Verify file inclusions |
| 206 | +- Check for unintended changes |
| 207 | +- Confirm test coverage |
| 208 | + |
| 209 | +### Mandatory Diff Review |
| 210 | + |
| 211 | +#### Pre-Commit Diff Check |
| 212 | + |
| 213 | +Before any commit, ALWAYS run: |
| 214 | + |
| 215 | + ```bash |
| 216 | + # Check status first |
| 217 | + git status | cat |
| 218 | + |
| 219 | + # Then review changes in detail |
| 220 | + git diff | cat |
| 221 | + |
| 222 | + # For staged changes |
| 223 | + git diff --staged | cat |
| 224 | + ``` |
| 225 | + |
| 226 | +#### Diff Review Checklist |
| 227 | + |
| 228 | +1. File Changes |
| 229 | + - Confirm all intended files are modified |
| 230 | + - No unintended file deletions |
| 231 | + - No sensitive files exposed |
| 232 | + |
| 233 | +2. Code Changes |
| 234 | + - Review each chunk of changes |
| 235 | + - Verify line deletions are intentional |
| 236 | + - Check for debugging code |
| 237 | + - Confirm formatting changes |
| 238 | + |
| 239 | +3. Large Changes Warning |
| 240 | + - If more than 500 lines changed, break into smaller commits |
| 241 | + - For large deletions, double-check necessity |
| 242 | + - Request confirmation for significant file removals |
| 243 | + |
| 244 | +4. Post-Review Actions |
| 245 | + - Address any unintended changes |
| 246 | + - Remove debug statements |
| 247 | + - Fix formatting issues |
| 248 | + - Split into multiple commits if needed |
0 commit comments