커밋 메시지를 작성하는 규약으로 사용자와 컴퓨터 모두에게 의미있고 이해하기 쉽도록 약속한 규악으로, 버전 관리 트레킹을 쉽게 만들어 줍니다.
커밋 메시지는 다음과 같이 구성됩니다.
<type>[optional scope]: <description>
[optional body]
[optional footer(s)]
Conventional Commits에는 의도를 전달하기 위해 다음과 같은 구조적 요소가 포함되어 있습니다.
fix: 코드베이스에서 버그를 패치하는 fix
type commit
feat: 코드베이스에서 새 기능이 추가되는 feat
type commit
BREAKING CHANGE: BREAKING CHANGE:
이라는 footer를 가지거나 타입/스코프 뒤에 !문자열을 붙인 커밋은 단절적 API 변경(breaking API change) 있다는 것을 의미합니다. 어떤 커밋 타입이라도 BREAKING CHANGE는 해당 커밋의 일부가 될 수 있습니다.
fix:
와 feat:
이 외의 다른 타입도 허용됩니다.
예를 들어 build:
, chore:
, ci:
, docs:
, style:
, refactor:
, perf:
, test:
등의 타입을 사용할 것을 권고하고 있습니다.
보다 유의미한 커밋을 남기는 것에도 의미가 있지만 release 자동화 package를 통해 commit message 기반의 버전 관리 및 패키징 퍼블리싱, ChangeLog 자동화에 대한 첫걸음이 됩니다.
huksy 설치 (이미 설치돼있으면 생략)
npm i -D husky
husky install
커밋 메시지 검사하기
npx husky add .husky/commit-msg 'node_modules/.bin/commitlint --edit $1'
npm i -D @commitlint/cli @commitlint/config-conventional
commitlint.config.js
파일 생성module.exports = {
extends: ['@commitlint/config-conventional'],
};
npx husky add .husky/prepare-commit-msg 'exec < /dev/tty && node_modules/.bin/cz --hook || true'
npm i -D commitizen
npm i -D cz-conventional-changelog
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
git commit
입력 시 conventional commits 을 도와주는 cli 사용 가능 (아래 이미지 참고)git commit -m
시 helper 안뜨게 하기converional commits helper cli 적용 시 git commit -m
옵션을 사용하여 커밋을 해도 helper가 뜨는 현상이 있는데 이 현상을 없애기 위해서는 아래 스크립트를 사용하면 됩니다.
npx husky add .husky/prepare-commit-msg '[ $2 == "template" ] && exec < /dev/tty && node_modules/.bin/cz --hook || true'
기존에 추가한 사람은 prepare-commint-msg script 앞에 [ $2 == "template" ] &&
를 추가해주면 된다.
githooks documentation을 보면 알 수 있는데 아래는 prepare-commit-msg githook에 대한 내용입니다.
간단하게 요약하자면 git commit 시 1~3개의 parameter가 내려오는 데 첫번째 파라미터는 파일명, 두번째 파라미터는 Commit Source 즉, 커밋이 호출된 조건이 들어있습니다.
이 중에 두번째 파라미터가 git commit
의 경우 template, git commit -m
의 경우 message로 넘어오는 것을 이용한 방식입니다.