简介
@ianwalter/faygit 是一个基于 Git 的命令行工具,用于在前端项目中管理和维护规范的 Commit message。它可以帮助团队成员按照约定的格式提交 Commit,提高代码可读性,帮助快速定位问题和跟踪代码变更。
安装和配置
安装
安装 @ianwalter/faygit 可以使用 npm,在终端执行以下命令:
npm install -g @ianwalter/faygit
配置
- 在项目根目录下新建
.faygitrc.json
文件,在文件中添加以下内容:
{ "format": "[{{type}}] {{message}}\n\n{{body}}\n\n{{breakingChange}}\n\n{{relatedIssues}}" }
其中,format
是一个包含格式占位符的字符串。在执行 fgit commit
命令时,将会根据这里的配置生成 Commit message。
- 在项目根目录下新建
commitlint.config.js
文件,在文件中添加以下内容:
module.exports = { extends: ['@commitlint/config-conventional'] };
这个文件配置了 @commitlint/config-conventional。它是 Commit message 格式规范的一个实现。
使用
在项目中,我们可以使用 fgit
命令来代替 git
命令。
fgit add
# 添加当前修改的所有文件到待提交区域 fgit add . # 添加单个文件到待提交区域 fgit add myfile.txt
fgit commit
# 提交 Commit message fgit commit
在执行 fgit commit
的时候,会提示你按照特定的规范填写 Commit message。比如,下面这个 Commit message:
[feat] 添加登录功能
- 添加登录页面,实现了用户登录功能
BREAKING CHANGE: 把
session
存储的方式从LocalStorage
改成了SessionStorage
。Resolves #123, #456.
其中 feat
表示新增功能,BREAKING CHANGE
表示提交的代码有破坏性改动,Resolves #123, #456
表示该 Commit 和 #123
、#456
这两个 Issue 的问题相关。具体的请参考 Angular Commit Message Conventions 中的定义。
fgit push
# 推送当前分支到远程仓库 fgit push
示例
我们假设当前项目有以下目录:
. ├── .faygitrc.json ├── commitlint.config.js ├── package.json ├── README.md └── src ├── App.js └── index.js
- 新建你的 feature 分支并切换到该分支
fgit checkout -b my-feature
- 修改代码
-- -------------------- ---- ------- -- ---------- ------ ----- ---- -------- - ----- -------- - -- -- - - ------ ------ ------- - - -------- ----- - ------ - ---- ---------------- - ----------- -- -- -------- - --------------------- ------ -- - ------ ------- ----
- 执行
fgit add
,将修改的代码添加到待提交区域
fgit add src/App.js
- 执行
fgit commit
,使用规范的 Commit message 提交代码
fgit commit
在执行 fgit commit
的时候,会提示你按照特定的规范填写 Commit message。
- 执行
fgit push
,将代用的 feature 分支推送到远程仓库
fgit push -u origin my-feature
总结
通过使用 @ianwalter/faygit,我们可以约束团队成员按照特定的规范提交 Commit message,提高代码可读性,帮助快速定位问题和跟踪代码变更。同时,项目中使用 @ianwalter/faygit 的过程也可以帮助团队成员熟练运用 Git 的相关命令。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/5f709e69a9b7065299ccbb1a