Git的一些實用基本操做, 這裏就不做複述了. 主要和你們分享如何更優雅的使用Gitgit
Git每次提交代碼, 都要寫Commit message, 不然提交不了. 咱們不只要寫commit message, 並且要寫得清晰明瞭, 以說明本次提交的目的.github
寫好commit message好處有不少, 如:shell
目前業界比較流行的commit規範, 主要包括三部分:npm
<type>(<scope>): <subject>
<blank line>
<body>
<blank line>
<footer>
複製代碼
提交的commit類型, 包括如下幾種:json
修改文檔的範圍, 好比: 視圖層, 控制層, docs, config, plugingulp
subject 是commit目的的一個簡短描述, 通常不超過50個字符bash
補充說明subject的, 能夠分紅多行, 適當增長緣由, 目的等相關因素, 也能夠不寫.markdown
validate-commit-msg Github於檢查項目的 Commit message 是否符合格式。 ghooks Github Simple git hooksapp
npm install --save-dev validate-commit-msg`
npm install ghooks --save-dev
複製代碼
在項目的根目錄下, 新建一個名爲.vcmrc
的文件, 並輸入如下內容:ide
{
"types": ["feat", "fix", "docs", "style", "refactor", "perf", "test", "build", "ci", "chore", "revert"],
"scope": {
"required": false,
"allowed": ["*"],
"validate": false,
"multiple": false
},
"warnOnFail": false,
"maxSubjectLength": 100,
"subjectPattern": ".+",
"subjectPatternErrorMsg": "subject does not match subject pattern!",
"helpMessage": "",
"autoFix": false
}
複製代碼
而後, 在package.json
中添加以下配置
{
…
"config": {
"ghooks": {
"pre-commit": "gulp lint",
"commit-msg": "validate-commit-msg",
"pre-push": "make test",
"post-merge": "npm install",
"post-rewrite": "npm install",
…
}
}
…
}
複製代碼
能夠根據項目的須要來添加, 例如:
"config": {
"ghooks": {
"commit-msg": "validate-commit-msg"
}
}
複製代碼
每次git commit的時候,這個腳本就會自動檢查 Commit message 是否合格。若是不合格,就會報錯。
$ git add -A
$ git commit -m "edit markdown" INVALID COMMIT MSG: does not match "<type>(<scope>): <subject>" ! was: edit markdown
複製代碼
工具commitizen能夠幫忙咱們寫出規範的commit message. Github
> npm install -g commitizen
複製代碼
···
commitizen init cz-conventional-changelog --save-dev --save-exact
以上命令將執行如下3個動做:
* 安裝cz-conventional-changelog依賴包
* 保存依賴包信息到package.json的dependencies七devDependencies中
* 添加config.commitizen字段到package.json中, 可能的配置以下:
``` json
"config": {
"commitizen": {
"path": "cz-conventional-changelog"
}
}
複製代碼
::: tip 若是之前使用過cz-conventional-changelog, 在末尾添加--force
參數, 強制更新package.json中的配置. 更多信息能夠經過命令commitizen help
得到 :::
接下來咱們就能夠愉快的使用git cz
命令來代替git commit
命令了.
E:\github\coding_docs>git cz
cz-cli@3.1.1, cz-conventional-changelog@2.1.0
Line 1 will be cropped at 100 characters. All other lines will be wrapped after 100 characters.
? Select the type of change that you're committing: feat: A new feature ? What is the scope of this change (e.g. component or file name)? (press enter to skip) gitCommit.md, package.json ? Write a short, imperative tense description of the change: add a new md file=> how to use git well ? Provide a longer description of the change: (press enter to skip) ? Are there any breaking changes? Yes ? Describe the breaking changes: how to use git well ? Does this change affect any open issues? No 複製代碼
本地安裝能夠確保項目的全部開發者都能執行相同的commitizen版本.
npm install commitizen --save-dev
npm commitizen init cz-conventional-changelog --save-dev --save-exact
複製代碼
或者
yarn add commitizen --dev
yarn commitizen init cz-conventional-changelog --dev --save-exact
複製代碼
手動添加以下配置至package.json文件中:
"script": {
"commit": "git-cz"
}
複製代碼
當須要commit文件時, 執行npm run commit
便可.
gitmoji 和 commitizen的做用都是幫助咱們寫出規範的commit message,不過gitmoji有更好玩的 moji表情。( 用moji來表示type ) Github
npm install -g gitmoji-cli
複製代碼
gitmoji -c
複製代碼
挑選個符合場景的moji提交本次更改:
與其餘版本控制系統同樣,當某些重要事件發生時,Git 能夠調用自定義腳本,Git 有不少鉤子能夠用來調用腳本自定義 Git。在 .git -> hooks 目錄下能夠看到示例。 例如:pre-commit就是在代碼提交以前作些事情。若是你打開了 hooks 目錄裏面的 *.sample 文件,你能夠看見裏面寫的shell腳本。可是我想用 Js 寫 hooks 咋辦?husky、pre-commit就能知足你。
如今咱們想實現一個提交代碼時使用 Eslint 進行代碼檢查的功能
npm install --save-dev pre-commit
複製代碼
在package.json 中配置pre-commit
"script": {
"lint": "eslint [options] [file|dir|glob|*]"
},
"pre-commit": [
"lint"
]
複製代碼
> git commit -m "test:Keep calm and commit"
複製代碼
> npm install husky@next --save-dev
複製代碼
和 pre-commit 同樣,仍是在package.json中配置。可是處理pre-commit鉤子它還能夠作的更多。
{
"scripts": {
"lint": "eslint [options] [file|dir|glob]*"
},
"husky": {
"hooks": {
"pre-commit": "npm lint",
"pre-push": "..."
}
}
}
複製代碼
若是你的全部 Commit 都符合 Angular 格式,那麼發佈新版本時, Change log 就能夠用腳本自動生成(例1,例2,例3)。
生成的文檔包括如下三個部分。
每一個部分都會羅列相關的 commit ,而且有指向這些 commit 的連接。固然,生成的文檔容許手動修改,因此發佈前,你還能夠添加其餘內容。 conventional-changelog 就是生成 Change log 的工具,運行下面的命令便可。
$ npm install -g conventional-changelog
$ cd my-project
$ conventional-changelog -p angular -i CHANGELOG.md -w
複製代碼
上面命令不會覆蓋之前的 Change log,只會在CHANGELOG.md的頭部加上自從上次發佈以來的變更。
若是你想生成全部發布的 Change log,要改成運行下面的命令。
$ conventional-changelog -p angular -i CHANGELOG.md -w -r 0
複製代碼
爲了方便使用,能夠將其寫入package.json的scripts字段。
{
"scripts": {
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -w -r 0"
}}
複製代碼
之後,直接運行下面的命令便可。
$ npm run changelog
複製代碼