Skip to content

Latest commit

 

History

History
48 lines (28 loc) · 1.61 KB

File metadata and controls

48 lines (28 loc) · 1.61 KB

Git Hooks

Hooks,為鉤子的意思。可以在 git action 執行前後 run your own script,常常會拿來做 unit test 檢查或是 linter 的 coding style 驗證。

Install git hooks

預設在 git 目錄底下就會有 hooks 的檔案

由於 .git 是在隱藏目錄下,使用 $ ll -la 來顯示

hooks01

接著只要進入 .git/hooks 底下就可以看到預設的 sample

hooks02

Use git hooks

當你只要修改了這些 .sample 檔案,把檔名換掉,git hooks 就會來執行這些檔案

舉例來說

我們去修改 pre-push.sample 裡面的 script,之後再把檔案從新命名為 pre-push ,這樣當你之後在執行 $ git push 之前,都會先來跑這隻 pre-push script。

因為只要 hooks 沒有 return 0 的時候,push action 就會失敗,所以 script 在實作上可能會寫成失敗 exit 1

npm run test || {
    echo Pre-push fail
    exit 1
}

實務上不論前後端開發,常常會用來綁定 test / linter 來檢查 code,像是 eslintfrontend unit testPMD linter ckeck 等等,目的讓團隊的 code 品質更好,預先減少很多錯誤的發生。

Bypass git hook

當然還是有辦法可以避開 hooks 的檢查,只要在 git action 的時候下參數就可以 bypass git hooks。

--no-verify

$ git push --no-verify

Reference