Git|Pre-Commit Hooks to Safeguard Code Quality
作成日:2025-11-01
更新日:2025-11-02

Before pushing your code to GitHub, it’s a good idea to run checks automatically.
pre-commit is a tool that does exactly that — it acts like a guardian to prevent messy or broken code from being committed.
What is pre-commit?
pre-commit automatically runs checks before each commit.
It ensures that only clean, well-formatted, and type-safe code passes through.
pre-commit vs GitHub Actions
| Tool | Timing | Purpose |
|---|---|---|
| pre-commit | Before committing | Runs checks locally before the code leaves your machine |
| GitHub Actions | After committing | Runs tests and workflows on GitHub’s servers |
Setup: Configuration File
Create a file named .pre-commit-config.yaml in the project root directory
(the same folder where .git exists).
This file defines what checks to run — such as formatting, linting, or type checking.
Example:
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.7
hooks:
- id: ruff
- id: ruff-format
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.10.0
hooks:
- id: mypy
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-yaml
- id: trailing-whitespaceMeaning of Each Key
| Key | Description |
|---|---|
repo | The GitHub repository that provides the hook |
rev | The version or tag of that repository |
hooks | The list of actions to run |
id | The specific hook or tool name |
args | Optional arguments (e.g. --fix to auto-format) |
Common Hook Repositories
| Repository | Purpose |
|---|---|
astral-sh/ruff-pre-commit | Code formatting and linting |
pre-commit/mirrors-mypy | Type checking |
pre-commit/pre-commit-hooks | Basic checks (YAML, whitespace, etc.) |
PyCQA/bandit | Security scanning |
PyCQA/isort | Sorting imports |
Setting Up
① Install hooks
pre-commit installThis creates a script in .git/hooks/pre-commit,
which tells Git to run the defined checks before committing.
② Run on all files (initial check)
pre-commit run --all-files 2>&1 | head -200--all-files→ runs checks on every filehead -200→ shows only the first 200 lines of output
During this process, tools like ruff (formatter) and mypy (type checker) are initialized.
After Setup
From now on, every time you run git commit,
the defined checks will run automatically — and your commit will be rejected if any errors are found.
Example:

Workflow Overview
- Write configuration in
.pre-commit-config.yaml - Run
pre-commit install(creates.git/hooks/pre-commit) - Commit your code
- Hooks execute automatically before the commit
- If errors exist, the commit is stopped
Summary
- pre-commit defines when the checks run (before commit).
- Linters and formatters define what is checked.
- The
.pre-commit-config.yamlfile lets you freely combine and customize these tools.
Result:
Only clean, safe, and consistent code gets committed.
Vocabulary
| English | Japanese |
|---|---|
| pre-commit hook | コミット前フック |
| run –all-files | 全ファイル実行 |
| linter | コードスタイルチェッカー |
| formatter | 自動整形ツール |
| environment initialization | 環境初期化 |
| hook installation | フックの設置 |
| commit gate | コミット時の品質ゲート |
Githubにアップする前に、チェックする
pre-commitとは
コミットする前に自動でコードをチェックしてくれるツール。
汚れたコードをコミットできないようにする見張り役のような存在。
- pre-commit:コミット前にチェックする
- GitHub Actions:コミット後にチェックする
設定ファイルを用意する
ファイル名: .pre-commit-config.yaml
.pre-commit-config.yamlを作る。
ここに、チェック内容(Lint・整形・型検査など)を書く。
これが「どんなテストを走らせるか」を決めるメニュー。
ファイルの置き場所
プロジェクトのルートディレクトリ(=.gitがある場所)
ファイルの中身(書き方サンプル)
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.7
hooks:
- id: ruff
- id: ruff-format
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.10.0
hooks:
- id: mypy
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-yaml
- id: trailing-whitespace各要素の意味
| 項目 | 内容 |
|---|---|
repo: | ツール定義を配布しているGitHubリポジトリ |
rev: | そのリポジトリのバージョン(Gitタグ推奨) |
hooks: | 実行する処理のリスト |
id: | 実際に動かすフック(ツール名など) |
args: | 追加のオプション(例:--fixで自動整形) |
repoとは|ツール定義を配布しているGitHubリポジトリ
pre-commitはあらかじめ決まったツールを持っているわけではない。
使いたいフックを自由に選んで使う。
= Gitフックの中でツールを呼び出す
代表的なフックリポジトリ
| 代表的なフックリポジトリ | |
|---|---|
astral-sh/ruff-pre-commit | コード整形・Lint |
pre-commit/mirrors-mypy | 型チェック |
pre-commit/pre-commit-hooks | 基本ルール(空白・YAMLなど) |
PyCQA/bandit | セキュリティチェック |
PyCQA/isort | import整列 |
設定ファイルの準備後、セットアップをする
① フックを登録する
pre-commit installこれで .git/hooks/pre-commit にスクリプトが作成される。
Gitがコミット前にこのスクリプトを実行する設定になる。
② 全ファイルをチェックする
pre-commit run --all-files 2>&1 | head -200--all-files:すべてのファイルをチェックするhead -200:結果のうち最初の200行だけを表示する- 実行時に
ruff(整形)やmypy(型チェック)などのツールが初期化される
セットアップが完了したら
chat_api.py などの既存コードに統合したり、CI/CD(自動テスト環境)に組み込んだりする。
3.git commit を実行するたびにチェックが入る
設定したチェックが自動で実行される。
例)

git commit -m をした後に、赤文字でErrorが表示される
フロー
.pre-commit-config.yamlに設定を書くpre-commit installで.git/hooks/pre-commitが生成される- コミット実行時に、指定したフック(ツール)が順に実行される
- エラーがあればコミットが止まる
まとめ
- pre-commitは「いつ実行するか(コミット前)」を担当する仕組み
- LintやFormatterなどのツールは「何を実行するか」を担当する
- どのツールを使うかは、
.pre-commit-config.yamlで自分で選んで組み合わせる
きれいで安全なコードだけがコミットを通過する仕組みになる。
Vocabulary
| English | Japanese |
|---|---|
| pre-commit hook | コミット前フック |
| run –all-files | 全ファイル実行 |
| linter | コードスタイルチェッカー |
| formatter | 自動整形ツール |
| environment initialization | 環境初期化 |
| hook installation | フックの設置 |
| commit gate | コミット時の品質ゲート |
2025-11-01
編集後記:
この記事の内容がベストではないかもしれません。