pre-commit은 Git 훅을 선언적으로 관리하는 도구다. 커밋 전에 린터, 포매터, 보안 스캔을 자동 실행하여 코드 품질을 강제한다. 다양한 언어의 훅을 YAML로 통합 관리한다.
설치 및 초기화
bash
# 설치
pip install pre-commit
# 또는
brew install pre-commit
# Git 훅 설치
pre-commit install
# 전체 파일에 수동 실행
pre-commit run --all-files
# 특정 훅만 실행
pre-commit run prettier --all-files
.pre-commit-config.yaml
yaml
repos:
# 기본 훅 모음
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-json
- id: check-merge-conflict
- id: detect-private-key
# Prettier (JS/TS 포매팅)
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.1.0
hooks:
- id: prettier
types_or: [javascript, jsx, typescript, tsx, css, markdown, yaml]
# ESLint
- repo: https://github.com/pre-commit/mirrors-eslint
rev: v8.56.0
hooks:
- id: eslint
files: \.tsx?$
types: [file]
# Python
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.9
hooks:
- id: ruff
args: [--fix]
- id: ruff-format
# 시크릿 감지
- repo: https://github.com/Yelp/detect-secrets
rev: v1.4.0
hooks:
- id: detect-secrets
args: ['--baseline', '.secrets.baseline']
CI에서 pre-commit 실행
yaml
# GitHub Actions
- name: pre-commit 검사
uses: pre-commit/action@v3.0.0
with:
extra_args: --all-files
관련 문서