Introduction to the
Commitizen Git Commit formatting tool provides standardized commit information. Help us unify the project commit for easy traceability of information or log generation. # commit message format
Commitizen is just a tool to format and interact with git. You need to output information that meets the commit rules
< type > (< scope >) : < subject > / / headers / / empty line < body > / / empty line < footer >Copy the code
- Header Information Header (mandatory)
- Type Commit type (mandatory)
- Scope commit Scope
- Subject COMMIT statement (required)
- Body Commit details
- Footer supporting information: 1. Incompatible changes 2. Close Issue
The installation
- First you need to install the Commitizen tool itself
// Install locally
npm i --save-dev commitizen
Copy the code
- Configuration commands
// package.json
{
"script": {
"commit": "cz"
}
}
Copy the code
Now let’s try calling a utility command
Git add * // Run NPM run commitCopy the code
Git commit > git commit > git commit In fact, Commitizen usually uses a rule module that needs to be matched. The commit information is normalized through the rules module. Default rule packs are provided by the authorities and the community.
- Installation Rule package
npx commitizen init cz-conventional-changelog --save-dev --save-exact
Copy the code
When the installation is complete, the Commitizen configuration will be added to package.json
The “path” here refers to the local configuration module file
Run NPM run commit
Custom Configuration
package.json
Modify the package.json config property to set the custom configuration
Here our custom configuration overrides the primitive type interaction.
The configuration file
By default, Commitizen loads the.czrc.czz. json configuration file in the root directory
// Add configuration file touch.cz.jsonCopy the code
Custom rule package
Similar to the package CZ-Conventional – Changelog, we can write a customized interactive rule package.
The new package
// root // create a directory md commit-rule // NPM init -y // add the entry file touch index.jsCopy the code
Writing package content
Commitizen calls the PROMPter function in the module to get the COMMIT information
function prompter(cz, commit) {
// CZ provides basic interaction capabilities to collect COMMIT information
// commit Commit the collected information
cz.prompt([
{
type: 'input'.name: 'type'.message: 'type'}, {type: 'input'.name: 'scope'.message: 'Scope of action'}, {type: 'maxlength-input'.name: 'subject'.message: 'profile'.maxLength: 20
}
]).then(answers= > {
const { type, scope, subject } = answers
const message = `
${type}(${scope}) :${subject}
time: The ${new Date().getTime()}
`
commit(message)
})
// Export regular functions
module.exports = {prompter} ### Import custom packages// .cz.json
{
"path": "./commit-rule"
}
Copy the code
Perform a commit
According to our custom rules, the COMMIT message was successfully committed
Here is a simple example of a simple writing process. We can also implement more detailed information rules or different interactive tools, such as: Inquirer Color, etc. After all, the front-end information collection format is customizable, and finally commit the information in accordance with Git rules.
Reference documentation
Commit Message and Change log writing guide – Ruan Yifong network log official Github