Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git 汇总 #10

Open
smallwebbird opened this issue Feb 11, 2023 · 0 comments
Open

git 汇总 #10

smallwebbird opened this issue Feb 11, 2023 · 0 comments

Comments

@smallwebbird
Copy link
Owner

校验提交

git hooks

所有钩子如下

applypatch-msg
 pre-applypatch
 post-applypatch
 pre-commit
 prepare-commit-msg
 commit-msg
 post-commit
 pre-rebase
 post-checkout
 post-merge
 pre-receive
 update
 post-receive
 post-update
 pre-auto-gc
 post-rewrite
 pre-push

每个钩子都会在不同阶段触发,当打开钩子文件,发现钩子文件是一个shell脚本,当在特定阶段触发对应的钩子,就会执行相应的shell脚本,下面通过一张图来看一下每个钩子都会在什么阶段触发

5b661b90-8c66-4176-bff0-4b82b955f77e_.png

当运行git init 创建git时,默认会在.git 目录下得hooks目录下生成所有的钩子文件

Untitled

默认这些钩子是不生效的,当去掉后缀sample 时,钩子会立刻生效

Husky

husky 是用来处理git hooks的,具体这里就不多说, husky 的v4版本和v6版本差距较大,具体的可以看[这篇文章](https://blog.csdn.net/qq_21567385/article/details/116429214)

CommitLint

commitlint 是用来校验git 提交信息,比如说commit代码时运行git commit -m ‘msg’,可以使用commitlint来校验msg格式是否符合规范

安装

npm install -D @commitlint/config-conventional @commitlint/cli

@commitlint/config-conventional commitlint 配置文件

@commitlint/cli commitlint cli命令

commitlint一般搭配husky使用

{
	"husky": {
		"commit-msg": "commitlint -E HUSKY_GIT_PARAMS" 
	}
}

当执行git commit -m “msg” 时触发commit-msg hooks,然后执行commitlint -E HUSKY_GIT_PARAMS 命令

@commitlint/cz-commitlint

这个包是用来代替git commit的

安装

npm install --save-dev @commitlint/cz-commitlint commitizen  

修改package.json

{
  "scripts": {
    "commit": "git-cz"
  },
  "config": {
    "commitizen": {
      "path": "@commitlint/cz-commitlint"
    }
  }
}

然后使用 npm run commit 来commit代码,commitlint 和 cz-commitlint 都是读取同一个配置文件

commitlint.config.js .commitlintrc.js ,可以自定义prompt,详细可以看[这篇文章](https://commitlint.js.org/#/reference-prompt)

commitizen

commitizen 通过问答的方式来定制化commit msg, 可以和git cz 一起使用

这里面具体讲一下在git commit 的时候去运行commitzen,根据上面的git hooks 可以知道,想要在git commit时运行相关代码的话,需要在git hooks中运行对应的脚本

  • 使用husky来管理hooks
  • 自己在.git 目录下创建对应hooks的shell脚本

对于husky,在 package.json中增加如下配置

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

对于自己管理hooks,.git/hooks/prepare-commit-msg

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

这里面 exec < /dev/tty 的作用是: 默认git hooks是不具有交互性的,这个命令允许用户在hooks阶段使用终端去和commitizen交互

关于 exec < /dev/tty 这行命令在windows 上运行, 目前没有找到好的解决方案,自己在本地试了一下,git hooks通过手动运行,shell脚本中的exec < /dev/tty 可以运行,但是通过git commit去触发对应的钩子函数中的exec < /dev/tty 无法运行

commitizen/cz-cli#627

cz-customizable

适合大型团队使用他们自己的提交范围处理多个项目。它允许您选择预定义的范围或提交类型

cz-customizable 目前有多种使用方法,而在这里要和commitizen 结合使用,可以在commitizen的配置文件增加如下代码 .czrc

{"path": "node_modules/cz-customizable"}

需要创建.cz-config.js, 这个配置文件是cz-customizable 的配置文件

cz-customizable 首先会查找临近package.json 的.cz-config.js 配置文件、.config/.cz-config.js 配置文件,如果没有找到,那么会去home目录下找.cz-config.js 配置文件、.config/.cz-config.js 文件,也可以在package.json中自定义配置文件

...
"config": {
  "commitizen": { // not needed for standlone usage
    "path": "node_modules/cz-customizable"
  },
  "cz-customizable": {
    "config": "config/path/to/my/config.js"
  }
}

git 相关操作

  • 撤销git add git reset HEAD .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant