콘텐츠로 이동

Husky Git Hooks 설정

Using lint-staged, husky, and pre-commit hooks to fail fast and early | by Dominic Fraser | codeburst

husky

Husky - Git hooks 문서

설치

To lint commits before they are created you can use Husky’s commit-msg hook:

Bash
# Install Husky v6  
npm install husky --save-dev  
# or  
yarn add husky --dev  

# Activate hooks  
npx husky install  
# or  
yarn husky install  

  • 옵션: 설정 파일을 지정하여 저장
    폴더이름을 따로 지정하지 않을 경우 .husky가 기본값으로 사용된다.
    Bash
    # `.husky/husky-config` 에 설정 파일 저장  
    npx husky install ".husky/husky-config"  
    

커밋 이전 Test 확인

  • 사용 방법
    Bash
    # pre-commit  
    npx husky add .husky/husky-config/pre-commit "npm test"  
    git add .husky/husky-config/pre-commit  
       
    # pre-push  
    npx husky add .husky/husky-config/pre-push "npm test"  
    git add .husky/husky-config/pre-push  
    

commit rule 강제하기

커밋 메시지 작성법 을 지키지 않으면 커밋 못하게 하는 설정법이다.

Text Only
feat: add hat wobble  
^--^  ^------------^  
|     |  
|     +-> Summary in present tense.  
|  
+-------> Type: chore, docs, feat, fix, refactor, style, or test.  

More Examples:

  • feat: (new feature for the user, not a new feature for build script)
  • fix: (bug fix for the user, not a fix to a build script)
  • docs: (changes to the documentation)
  • style: (formatting, missing semi colons, etc; no production code change)
  • refactor: (refactoring production code, eg. renaming a variable)
  • test: (adding missing tests, refactoring tests; no production code change)
  • chore: (updating grunt tasks etc; no production code change)

GitHub - conventional-changelog/commitlint: 📓 Lint commit messages 를 참고해서 세팅하면 된다.
commitlint - Lint commit messages

getting start

Bash
# Install commitlint cli and conventional config  
npm install @commitlint/{config-conventional,cli} --save-dev  


yarn add @commitlint/{config-conventional,cli} --dev  
# For Windows:  
npm install --save-dev @commitlint/config-conventional @commitlint/cli  

# Configure commitlint to use conventional config  
echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js  

Add hook

Bash
npx husky add .husky/commit-msg  'npx --no -- commitlint --edit ${1}'  

Test simple usage

For a first simple usage test of commitlint you can do the following:

Bash
npx commitlint --from HEAD~1 --to HEAD --verbose  

This will check your last commit and return an error if invalid or a positive output if valid.

Test the hook

Bash
git commit -m "foo: this will fail"  
husky > commit-msg (node v10.1.0)  
No staged files match any of provided globs.  
   input: foo: this will fail  
   type must be one of [build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test] [type-enum]  

   found 1 problems, 0 warnings  
   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint  

husky > commit-msg hook failed (add --no-verify to bypass)  

Pasted image 20230125175637.png

테스트 통과 확인 및 앵귤러 커밋 규칙을 지켰는지 확인 후 커밋한다.

  • yarn 2+ 셋업: Husky - Git hooks
    Bash
    yarn dlx husky-init --yarn2 && yarn # Yarn 2+  
    
    yarn husky add .husky/commit-msg 'yarn commitlint --edit $1'  
    

참고 문서

Best way to baseline nestjs microservice – @tkssharma | Tarun Sharma | My Profile
husky 로 git hook 하자 | 가비아 라이브러리
husky 적용 방법 | Dan DevLog


마지막 업데이트 : 2025년 4월 23일
작성일 : 2023년 1월 26일