優雅的使用Git

Git的一些實用基本操做, 這裏就不做複述了. 主要和你們分享如何更優雅的使用Gitgit

寫好commit message

Git每次提交代碼, 都要寫Commit message, 不然提交不了. 咱們不只要寫commit message, 並且要寫得清晰明瞭, 以說明本次提交的目的.github

寫好commit message好處有不少, 如:shell

  • 統一項目組的Git commit 日誌風格
  • 便於往後code review
  • 便於收集Change log

Commit 提交規範

目前業界比較流行的commit規範, 主要包括三部分:npm

  • header
  • body
  • footer 完整格式以下:
<type>(<scope>): <subject>
<blank line>

<body>
<blank line>
<footer>
複製代碼

type

提交的commit類型, 包括如下幾種:json

  • feat: 新功能
  • fix: 修復問題
  • docs: 修改文檔
  • style: 修改代碼格式, 不影響代碼邏輯
  • refactor: 重構代碼, 理論上不影響現有功能
  • perf: 提高性能
  • test: 增長修改測試用例
  • chore: 修改工具相關(包括但不限於文檔, 代碼生成等)

scope

修改文檔的範圍, 好比: 視圖層, 控制層, docs, config, plugingulp

subject

subject 是commit目的的一個簡短描述, 通常不超過50個字符bash

body

補充說明subject的, 能夠分紅多行, 適當增長緣由, 目的等相關因素, 也能夠不寫.markdown

footer

  • 當有非兼容修改時必須在這裏描述清楚
  • 關閉issue或是連接到相關文檔, 如: Closes #111923, Closes #93201

validate-commit-msg ghooks

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

工具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

gitmoji 和 commitizen的做用都是幫助咱們寫出規範的commit message,不過gitmoji有更好玩的 moji表情。( 用moji來表示type ) Github

安裝

npm install -g gitmoji-cli
複製代碼

使用

gitmoji -c
複製代碼

挑選個符合場景的moji提交本次更改:

使用Git hooks

與其餘版本控制系統同樣,當某些重要事件發生時,Git 能夠調用自定義腳本,Git 有不少鉤子能夠用來調用腳本自定義 Git。在 .git -> hooks 目錄下能夠看到示例。 例如:pre-commit就是在代碼提交以前作些事情。若是你打開了 hooks 目錄裏面的 *.sample 文件,你能夠看見裏面寫的shell腳本。可是我想用 Js 寫 hooks 咋辦?husky、pre-commit就能知足你。

如今咱們想實現一個提交代碼時使用 Eslint 進行代碼檢查的功能

pre-commit GitHub

安裝

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"
複製代碼

husky Github

安裝

> 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": "..."
    }
  }
}
複製代碼

生成change log

若是你的全部 Commit 都符合 Angular 格式,那麼發佈新版本時, Change log 就能夠用腳本自動生成(例1,例2,例3)。

生成的文檔包括如下三個部分。

  • New features
  • Bug fixes
  • Breaking changes.

每一個部分都會羅列相關的 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
複製代碼
相關文章
相關標籤/搜索