📖 blog post:Git commit (interactive Command line)

As the size of the project increases, more and more students are involved in the project. Everyone has his/her own habit of making Git log:

  • Format 1:Add: add...
  • Format 2:[add] : add...
  • Format:Add to Add...

Git commit records need to be regulated in order to form a unified specification, reach consensus, and reduce collaborative development costs.

Standardize git commit records

To standardize git commit logging, you need to do two things:

  • The interactive command line automatically generates COMMIT records that conform to the specified specification
  • After committing the record, do a commit record format check in Git hooks

Q: Why do you need to check in hooks now that you’ve interactively generated the specification record?

Interactive generation of commit records requires the user to invoke custom NPM scripts, such as NPM Run commit. However, you can commit records directly by calling the native Git command git commit. The check is performed before the official commit, so records that do not meet the requirements do not take effect and need to be recommitted.

Investigate: An interactive Commit log specification solution

As a result of preliminary research, there are two approaches to commit prompts:

  1. Use the adapter commonly used in Commitizen directly
  2. Customize the Adapter according to your team’s needs

Advantages and disadvantages of Method 1:

Advantage 1: You can install the adapter directly

Advantage 2: No development costs

Disadvantage 1: It cannot be customized and may not meet the needs of the team

Advantages and disadvantages of Method 2:

Advantage 1: Customizable to meet development requirements

Advantage 2: Independent library, release TNPM, as technology construction

Disadvantage 1: Need for a separate warehouse (but not expensive to develop)

Code implementation

In practice, common specifications in Approach 1 were found to be sufficient to cover the team’s daily development scenarios. So, method 1 is chosen.

Step1: install the NPM package

npm i --save-dev commitizen cz-conventional-changelog @commitlint/cli @commitlint/config-conventional husky
Copy the code

Add package.json configuration:

"scripts": {
    "commit": "git-cz"
},
"husky": {
    "hooks": {
        "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
    }
},
"config": {
    "commitizen": {
      "path": "./node_modules/cz-conventional-changelog"
    }
}
Copy the code

Create commitlint.config.js in the root directory of the project:

module.exports = {
    extends: ["@commitlint/config-conventional"]};Copy the code

Git commit -m… Instead, call NPM Run commit.

👇 scan code to pay attention to the “xin Tan blog”, view the “front-end atlas” & “algorithm problem solving”, adhere to sharing, grow together 👇