Use Git more elegantly

How to use Git more elegantly?

Write a commit message

Every time Git submits code, it must write a Commit message, otherwise it will not be submitted. Not only do we have to write a Commit message, but we should also write it clearly and clearly, explaining the purpose of this commit.

$ git commit -m "提交信息"

Write a commit message in the editor

$ git commit

There are many benefits of writing a good Commit message:

1. Unified team Git commit log style

2. It is convenient for future Reviewing Code

3. Help us write Changelog

4. It can improve the overall quality of the project

Commit commit specification

The industry respects Angular's commit specification http://suo.im/4rsYee

Commit message consists of three parts: Header, Body and Footer. The full format is as follows:

<type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>

1) type

Submit the type of commit, including the following

- feat: new function

- fix: fix issues

- docs: modify documentation

- style: Modify the code format without affecting the code logic

- refactor: refactoring code, theoretically does not affect existing functionality

- perf: improve performance

- test: add and modify test cases

- chore: Modify tool related (including but not limited to documentation, code generation, etc.)

2) scope

Modify the scope of the file, such as: view layer, control layer, docs, config, plugin

3) subject

- subject is a short description of the purpose of the commit (one sentence clearly describing what the commit did), no more than 50 characters

4) body

- Supplementary subject to add detailed description, which can be divided into multiple lines, and appropriately add relevant factors such as reason, purpose, etc., or not write

5 ) footer

- When there are incompatible changes (Breaking Change), it must be clearly described here

- Close the issue or link to related documentation, such as Closes #1, Closes #2, #3

use commitzen

The commitzen tool can help us write standard Commit messages.

GitHub:https://github.com/commitizen/cz-cli

img

Install globally using npm

$ npm install -g commitizen

Use angular's commit specification in your project

$ commitizen init cz-conventional-changelog --save-dev --save-exact

Then we can happily use git cz instead of the git commit command. Of course, we can also add it to the npm script

"script": {
    "commit": "git cz"
}

Then use npm run commit directly

use gitmoji

The role of gitmoji and commitzen is to help us write standard commit messages, but gitmoji has more fun moji expressions. (Use moji to represent type)

GitHub:https://github.com/carloscuesta/gitmoji-cli

Install and use

# 安装
$ npm i -g gitmoji-cli
# 使用
$ gitmoji -c

Pick a moji that matches the scene and submit this change

img

Using Git hooks

Like other version control systems, Git can call custom scripts when certain important events happen, and Git has many hooks that can be used to call scripts to customize Git. Examples can be seen in the .git -> hooks directory. For example: pre-commit is to do something before the code is committed. If you open the *.samplefile , you can see the shell script written inside. But what if I want to write hooks with Js? husky, pre-commit can satisfy you.

Now we want to implement a function that uses Eslint for code inspection when submitting code

First look at pre-commit

GitHub:https://github.com/observing/pre-commit

# 下载安装
npm install --save-dev pre-commit

Configure pre-commit in package.json

"scripts": {    
   "lint": "eslint [options] [file|dir|glob]*",
},
"pre-commit": [    
   "lint",
]

Submit the code to try now

git commit -m 'Keep calm and commit'

try husky again

GitHub:https://github.com/typicode/husky

It's still a download at the beginning, but here we use the @next version, and the usage method is slightly different from the official version.

npm install husky@next --save-dev

Like pre-commit, it is still configured in package.json. But it can do much more with pre-commit hooks.

// package.json
{
  "scripts": {
    "lint": "eslint [options] [file|dir|glob]*",
  },
  "husky": {
    "hooks": {
      "pre-commit": "npm lint",
      "pre-push": "..."
    }
  }
}

This article introduces how to write a standard Commit message, and use the two tools commentzen and gitmoji to help us write a standard Commit message. Finally, it introduces Git hooks, and uses husky or pre-commit in combination with Eslint to build a code inspection workflow. Of course, you can do more.

End of this article

Welcome to follow my official account and play together. There are technical dry goods and nonsense, pay attention to the reply [888] to receive a surprise

Left-hand code, right-hand brick, throwing bricks and attracting jade

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325966002&siteId=291194637