优雅的git commit

优雅的git commit

npm install -g commitizen

写好 commit message

Git 每次提交代码,都要写 Commit message,否则提交不了。我们不光得写 Commit message 而且还应该写的清晰明了,说明本次提交的目的。

$ git commit -m “提交信息”

在编辑器中写commit message

$ git commit

写好 Commit message 好处多多:

1、统一团队Git commit 日志风格

2、方便日后 Reviewing Code

3、帮助我们写好 Changelog

4、能很好的提升项目整体质量

Commit 提交规范

业界比较推崇 Angular 的 commit 规范 http://suo.im/4rsYee

Commit message 包括三个部分:Header,Body 和 Footer。完整格式如下:

():

1) type

提交 commit 的类型,包括以下几种

  • feat: 新功能

  • fix: 修复问题

  • docs: 修改文档

  • style: 修改代码格式,不影响代码逻辑

  • refactor: 重构代码,理论上不影响现有功能

  • perf: 提升性能

  • test: 增加修改测试用例

  • chore: 修改工具相关(包括但不限于文档、代码生成等)

2) scope

修改文件的范围,比如:视图层、控制层、docs、config, plugin

3) subject

  • subject 是 commit 目的的简短描述(用一句话清楚的描述这次提交做了什么),不超过50个字符

4) body

  • 补充 subject 添加详细说明,可以分成多行,适当增加原因、目的等相关因素,也可不写

5 ) footer

  • 当有非兼容修改(Breaking Change)时必须在这里描述清楚

  • 关闭issue或是链接到相关文档,如 Closes #1, Closes #2, #

commit工具Commitizen

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

Installing the command line tool

Commitizen is currently tested against Node.js 12, 14, & 16, although it may work in older versions of Node.js. You should also have npm 6 or greater.

Installation is as simple as running the following command (if you see EACCES error, reading fixing npm permissions may help):

npm install -g commitizen

Using the command line tool

If your repo is Commitizen friendly:

Simply use git cz or just cz instead of git commit when committing. You can also use git-cz, which is an alias for cz.

Alternatively, if you are using npm 5.2+ you can use npx instead of installing globally:

npx cz

or as an npm script:

  ...
  "scripts": {
    "commit": "cz"
  }

When you’re working in a Commitizen-friendly repository, you’ll be prompted to fill in any required fields, and your commit messages will be formatted according to the standards defined by project maintainers.

Add and commit with Commitizen

If your repo is NOT Commitizen friendly:

If you’re not working in a Commitizen-friendly repository, then git cz will work just the same as git commit, but npx cz will use the streamich/git-cz adapter. To fix this, you need to first make your repo Commitizen friendly

Making your repo Commitizen friendly

For this example, we’ll be setting up our repo to use AngularJS’s commit message convention, also known as conventional-changelog.

First, install the Commitizen CLI tools:

npm install commitizen -g

Next, initialize your project to use the cz-conventional-changelog adapter by typing:

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

Or if you are using Yarn:

commitizen init cz-conventional-changelog --yarn --dev --exact

Note that if you want to force install over the top of an old adapter, you can apply the --force argument. For more information on this, just run commitizen help.

The above command does three things for you:

  1. Installs the cz-conventional-changelog adapter npm module
  2. Saves it to package.json’s dependencies or devDependencies
  3. Adds the config.commitizen key to the root of your package.json file as shown here:
...
  "config": {
    "commitizen": {
      "path": "cz-conventional-changelog"
    }
  }

Alternatively, Commitizen configs may be added to a .czrc file:

{
  "path": "cz-conventional-changelog"
}

This just tells Commitizen which adapter we actually want our contributors to use when they try to commit to this repo.

commitizen.path is resolved via require.resolve and supports:

  • npm modules
  • directories relative to process.cwd() containing an index.js file
  • file base names relative to process.cwd() with .js extension
  • full relative file names
  • absolute paths

Please note that in the previous version of Commitizen we used czConfig. czConfig has been deprecated, and you should migrate to the new format before Commitizen 3.0.0.

Optional: Install and run Commitizen locally

Installing and running Commitizen locally allows you to make sure that developers are running the exact same version of Commitizen on every machine.

Install Commitizen with npm install --save-dev commitizen.

On npm 5.2+ you can use npx to initialize the conventional changelog adapter:

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

For previous versions of npm (< 5.2) you can execute ./node_modules/.bin/commitizen or ./node_modules/.bin/cz in order to actually use the commands.

You can then initialize the conventional changelog adapter using: ./node_modules/.bin/commitizen init cz-conventional-changelog --save-dev --save-exact

And you can then add some nice npm scripts in your package.json file pointing to the local version of Commitizen:

  ...
  "scripts": {
    "commit": "cz"
  }

This will be more convenient for your users because then if they want to do a commit, all they need to do is run npm run commit and they will get the prompts needed to start a commit!

NOTE: If you are using precommit hooks thanks to something like husky, you will need to name your script something other than "commit" (e.g. "cm": "cz"). The reason is because npm scripts has a “feature” where it automatically runs scripts with the name prexxx where xxx is the name of another script. In essence, npm and husky will run "precommit" scripts twice if you name the script "commit", and the workaround is to prevent the npm-triggered precommit script.

Optional: Running Commitizen on git commit

This example shows how to incorporate Commitizen into the existing git commit workflow by using git hooks and the --hook command-line option. This is useful for project maintainers who wish to ensure the proper commit format is enforced on contributions from those unfamiliar with Commitizen.

Once either of these methods is implemented, users running git commit will be presented with an interactive Commitizen session that helps them write useful commit messages.

NOTE: This example assumes that the project has been set up to use Commitizen locally.

Traditional git hooks

Update .git/hooks/prepare-commit-msg with the following code:

#!/bin/bash
exec < /dev/tty && node_modules/.bin/cz --hook || true
Husky

For husky users, add the following configuration to the project’s package.json file:

"husky": {
  "hooks": {
    "prepare-commit-msg": "exec < /dev/tty && npx cz --hook || true"
  }
}

Why exec < /dev/tty? By default, git hooks are not interactive. This command allows the user to use their terminal to interact with Commitizen during the hook.

Congratulations! Your repo is Commitizen friendly. Time to flaunt it!

Add the “Commitizen friendly” badge to your README using the following markdown:

[[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xZbKWSTZ-1656073639959)(https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)]](http://commitizen.github.io/cz-cli/)

Your badge will look like this:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xpCswpXs-1654111973170)(https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)]

It may also make sense to change your README.md or CONTRIBUTING.md files to include or link to the Commitizen project so that your new contributors may learn more about installing and using Commitizen.

Conventional commit messages as a global utility

Install commitizen globally, if you have not already.

npm install -g commitizen

Install your preferred commitizen adapter globally (for example cz-conventional-changelog).

npm install -g cz-conventional-changelog

Create a .czrc file in your home directory, with path referring to the preferred, globally-installed, commitizen adapter

echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc

You are all set! Now cd into any git repository and use git cz instead of git commit, and you will find the commitizen prompt.

Pro tip: You can use all the git commit options with git cz. For example: git cz -a.

If your repository is a Node.js project, making it Commitizen friendly is super easy.

If your repository is already Commitizen friendly, the local commitizen adapter will be used, instead of globally installed one.

Commitizen for multi-repo projects

As a project maintainer of many projects, you may want to standardize on a single commit message format for all of them. You can create your own node module which acts as a front-end for Commitizen.

1. Create your own entry point script

// my-cli.js

#!/usr/bin/env node
"use strict";

const path = require('path');
const bootstrap = require('commitizen/dist/cli/git-cz').bootstrap;

bootstrap({
  cliPath: path.join(__dirname, '../../node_modules/commitizen'),
  // this is new
  config: {
    "path": "cz-conventional-changelog"
  }
});

2. Add the script to your package.json file

// package.json

{
  "name": "company-commit",
  "bin": "./my-cli.js",
  "dependencies": {
    "commitizen": "^2.7.6",
    "cz-conventional-changelog": "^1.1.5"
  }
}

3. Publish it to npm and use it!

npm install --save-dev company-commit

./node_modules/.bin/company-commit

commit相关

https://www.zhihu.com/question/21209619/answer/257574960

https://juejin.cn/post/6844903606815064077

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一行1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值