Conventional Commits
See: Conventional Commits
yarn add husky @commitlint/cli @commitlint/config-conventional -D -W
yarn husky init
echo "npx --no -- commitlint --edit \$1" > .husky/commit-msg
Commitlint Configuration
cat << 'EOF' > commitlint.config.js
// Create commitlint.config.js at the root:
// Note: We can customize the rules based on our .
module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
'type-enum': [
2,
'always',
[
'feat', // Features
'fix', // Bug fixes
'docs', // Documentation changes
'style', // Code style (formatting)
'refactor', // Refactoring
'test', // Adding or updating tests
'chore', // Other changes (e.g., deps, build)
'ci', // CI/CD changes
'perf' // Performance improvements
]
],
'subject-case': [2, 'always', 'lowercase'], // Ensure proper capitalization
'header-max-length': [2, 'always', 140] // Max header length
}
};
EOF
Test Your Commits
npx commitlint --from HEAD~1 --to HEAD --verbose
Fixing Commit Messages
To update the commit message of your most recent commit to comply with the commitlint rule (ensuring the subject is in sentence case), you can amend the commit message using the following command:
-
Amend the most recent commit message:
git commit --amend -m "docs(setup): Sync workspace to repo"- This command opens the latest commit message in an editor, allowing you to change it. By passing
-m, you provide a new message directly.
- This command opens the latest commit message in an editor, allowing you to change it. By passing
-
Force push the amended commit (if it’s already pushed to remote):
-
If the commit has been pushed to a remote repository, you'll need to force push to update the remote commit:
git push --force -
Be cautious with force pushing, especially if others are also working off the same branch, as it can overwrite changes in the shared commit history.
-
This will update the commit message to comply with the sentence-case rule required by commitlint. After making changes, you can run your commitlint check again to confirm the fix:
npx commitlint --from HEAD~1 --to HEAD --verbose
This should pass the commitlint check if the message is amended correctly.
Git Editor Configuration
To set Visual Studio Code as your default editor for Git commit messages and other Git operations, you can configure Git to use VS Code as your editor. Here's how you can do it:
-
Open the Terminal.
-
Configure Git to use Visual Studio Code as the default editor: Execute the following command:
git config --global core.editor "code --wait"--global: Applies this configuration globally for your user.core.editor: This sets the default editor for Git.code --wait: This command uses Visual Studio Code as the editor. The--waitflag tells Git to wait until you close the editor window before proceeding. This is important for operations likegit commit, where the commit is finalized after you edit and save the message.
-
Verify the configuration: You can check if the change was applied correctly by running:
git config --global --get core.editor- This command should output
code --wait, confirming that VS Code is set as the default editor.
- This command should output
After setting this, whenever you perform an action requiring text input, like committing (git commit) without the -m flag, Git will open Visual Studio Code for you to enter your input. If VS Code is already open, it will use an existing window for the commit message. If it isn’t, it will open a new window.
This setting is particularly helpful if you're not comfortable with terminal-based editors like Vim or Nano, allowing you to use the familiar VS Code environment instead.