Analyze the changes and create a commit in Conventional Commits format.
Flow
Step 1: Check the changes
git status
git diff --staged
If there is nothing staged, also check git diff and guide the user to stage.
Step 2: Secret inclusion check (required)
Before executing the commit, confirm that no confidential information is included in the staged diff.
The check consists of two stages: (a) mechanical matching と (b) visual review . (a) alone is insufficient, and (b) must always be performed even if (a) detects nothing(the reason is given later in "Limitations of mechanical matching").
(a) Mechanical matching
Define the pattern only once in a variable, and use the same string for the self-test and the actual check. grep implementation differences
(BSD grep / GNU grep / ugrep) can cause the interpretation of BRE \+ to diverge, and the regular expression may become a syntax error.
The grep that errored terminates with a non-zero status, so with the form... || echo "検出なし" ,even if secret information is present,
it is reported as "not detected"(fail-open). This applies to both the detection side (grep -E) as well asand the exclusion side (grep -vE) with the
same, and if the exclusion pattern breaks, already-matched lines disappear and it becomes "not detected". Run the self-test
including the exclusion side as well.
Do not split the definition and the check into separate code blocks. Code blocks may each be executed in an independent shell, so variables are not carried over, and it creates room for "untested regular expressions" to arise. Make the following complete within a single block.
# ---- パターン定義(自己テストと本検査で共有する。変更はここだけで行う)----
# 認証情報ファイル名と、テンプレート・公開鍵の除外
RE_CREDFILE='(^|/)\.env(\..+)?$|(^|/)(id_rsa|id_ed25519)(\..+)?$|\.(pem|p12|pfx|key)$'
RE_CREDFILE_OK='\.(example|sample|template|pub)$'
# 高信頼パターン: ベンダー固有のトークン形式・秘密鍵ヘッダー・JWT(3 セグメント)・
# 認証情報を埋め込んだ接続文字列(scheme://user:pass@host)
RE_HIGH='sk-[A-Za-z0-9_-]{20,}|ghp_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,}|AKIA[0-9A-Z]{16}|xox[baprs]-[A-Za-z0-9-]+|AIza[0-9A-Za-z_-]{35}|BEGIN (RSA |EC |OPENSSH |PGP )?PRIVATE KEY|eyJ[A-Za-z0-9_=-]{8,}\.eyJ[A-Za-z0-9_=-]{8,}\.[A-Za-z0-9_=-]{8,}|[a-zA-Z][a-zA-Z0-9+.-]*://[^[:space:]/@]+:[^[:space:]/@]+@'
# 代入形パターン: 「鍵名 + 区切り + 値」。`[a-z_]*token` は `-i` と併せて GITHUB_TOKEN /
# GH_TOKEN / access_token / auth_token / csrf_token を一括で拾う。鍵名を個別に列挙すると
# 必ず取りこぼすため、token 系は接頭辞を問わない形にしておく(誤検知は (b) で捌く)
RE_ASSIGN='(password|passwd|secret|client_secret|api_?key|[a-z_]*token|aws_secret_access_key|private_?key)[[:space:]]*[:=][[:space:]]*[^[:space:]]{8,}'
# 代入形の除外: 環境変数参照・プレースホルダ・例示値
RE_ASSIGN_OK='\$\{|\$\(|%[A-Za-z_]+%|process\.env|os\.environ|ENV\[|getenv|<[A-Za-z_][A-Za-z0-9_-]*>|\*\*\*|REDACTED|redacted|CHANGEME|changeme|placeholder|PLACEHOLDER|dummy|DUMMY|xxxx|XXXX|example\.com|your[-_]?(password|secret|key|token)'
# ---- 自己テスト(1 つでも外したら以降の検査結果は信用できない)----
# 除外側は「実値が生き残ること」を先に確かめる。この向きでないと、除外パターンの構文エラーで
# grep が非ゼロ終了した場合に「除外された」と区別できず、テスト自体が fail-open する
{
printf '+++ b/x\n+password: Hunter2Hunter2Hunter2\n' \
| grep -E '^\+' | grep -vE '^\+\+\+' | grep -qiE "${RE_ASSIGN}" \
&& tok="notarealvalue""0123456789abcdef" \
&& printf '+GITHUB_TOKEN=%s\n' "${tok}" | grep -qiE "${RE_ASSIGN}" \
&& printf '+GITHUB_TOKEN=%s\n' "${tok}" | grep -vE "${RE_ASSIGN_OK}" | grep -q . \
&& printf '+db: postgres://u:p@h:5432/d\n' | grep -qE "${RE_HIGH}" \
&& printf '+t: eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dBjftJeZ4CVPmB92K27u\n' \
| grep -qE "${RE_HIGH}" \
&& printf '.env\n' | grep -E "${RE_CREDFILE}" | grep -vE "${RE_CREDFILE_OK}" | grep -q . \
&& [ -z "$(printf '.env.example\n' | grep -E "${RE_CREDFILE}" | grep -vE "${RE_CREDFILE_OK}")" ] \
&& printf '+password: Hunter2Hunter2Hunter2\n' | grep -vE "${RE_ASSIGN_OK}" | grep -q . \
&& [ -z "$(printf '+password: ${DB_PASSWORD}\n' | grep -vE "${RE_ASSIGN_OK}")" ]
} || { echo "エラー: シークレット検査の正規表現が機能していない(grep 実装差の可能性)。コミットを中止する" >&2; exit 1; }
# ---- 検査 ----
# 1. 認証情報ファイルの検出(.env・秘密鍵等)。追加・変更(--diff-filter=ACMR)のみを対象とし、
# 削除コミット(漏洩した .env の除去等の是正コミット)はブロックしない。
git diff --staged --name-only --diff-filter=ACMR \
| grep -E "${RE_CREDFILE}" \
| grep -vE "${RE_CREDFILE_OK}" || echo "認証情報ファイル: 検出なし"
# 2. 高信頼パターンの検出。追加行(^+。ファイルヘッダ +++ は除外)のみを対象とし、
# 漏洩済みシークレットの削除(- 行)を妨げない。
# 一致したら誤検知の可能性が低いため、原則そのまま中止する。
git diff --staged --diff-filter=ACMR \
| grep -E '^\+' | grep -vE '^\+\+\+' \
| grep -E "${RE_HIGH}" || echo "高信頼パターン: 検出なし"
# 3. 代入形パターンの検出。値の形式は問わないため誤検知が出やすく、
# 環境変数参照・プレースホルダは除外したうえで、残ったものを**ユーザーへ提示して確認**する。
# (除外側を広く取っているため、ここでの「検出なし」は不在の証明にはならない。(b) を必ず行う)
git diff --staged --diff-filter=ACMR \
| grep -E '^\+' | grep -vE '^\+\+\+' \
| grep -iE "${RE_ASSIGN}" \
| grep -vE "${RE_ASSIGN_OK}" || echo "代入形パターン: 検出なし"
# 4. 認証情報ファイルのリネームの検出。上記の名前照合はリネーム後の新パスにしか働かないため、
# 旧パスが認証情報パターンに一致するリネーム(例: .env → config.txt)を別途検出する。
# .env の中身(KEY=VALUE 形式)は高信頼パターンに一致しないことが多く、
# 名前照合が主防御線であるため、名前を変えて内容が残るケースを見逃さない。
git diff --staged --name-status --diff-filter=R \
| cut -f2 \
| grep -E "${RE_CREDFILE}" \
| grep -vE "${RE_CREDFILE_OK}" || echo "認証情報ファイルのリネーム: 検出なし"
If any of 1, 2, or 4 is detected,abort the commit, warn the user, and guide them to unstage the relevant file or remove the relevant lines. Only when it can be judged to be a false positive such as an example value or placeholder, proceed after obtaining the user's explicit confirmation.
If 3 is detected, do not abort;present the relevant line to the user and confirm whether it is a real value, and then make a judgment (if it is a real value, abort the commit). 3 is not set to auto-abort because its false-positive rate is structurally high.
If a rename (4) is detected, confirm whether the secret content remains under the new filename,
and abort if it remains. A commit that only deletes (.env deletion or removal of a leaked key) is
not subject to detection and may proceed as is, butwhen deletion (D) of a credentials file and addition (A) of a differently named file are
included in the same commitFirst, check via the diff whether the content has not been re-added under a different name (i.e., whether it is not effectively a rename),
then continue.
(b) Visual review (mandatory even if the mechanical check returns "no detection")
# 追加行のみを対象に、秘密情報の観点で staged 差分を読む
git diff --staged --diff-filter=ACMR | grep -E '^\+' | grep -vE '^\+\+\+'
Read the added lines and judge whether they contain secret information from the following perspectives.
| Perspective | Example |
|---|---|
| Credentials with arbitrary names | dbPass・clientKey・token2 etc., variable names not in the above key name list |
| Actual values in structured files | config.yml / settings.json / *.tf passwords written directly in / k8s manifest |
| High-entropy strings | Long random strings of unknown purpose (base64, hex) |
| Fragments of connection information | Places where host name, port, and user name appear together with their values |
| Binary / encoded | Credentials encoded in base64 (such as in a k8s Secret's data: ) |
If determined to be secret information, or if the determination cannot be made,Abort the commitand present the relevant location to the user for confirmation.
Limitations of mechanical checking (the contract of this procedure)
(a) is pattern matching and isnot exhaustive detection. The following cannot be detected in principle.
- Credentials assigned to arbitrary variable names (ones not on the key name list)
- Credentials whose value format has no regularity (many company-issued tokens, DB passwords)
- Actual values that happen to match one of the exclusion patterns in 3
Therefore, the contract of Step 2 is not "safe if (a) does not detect it," but "(a) mechanically removes the obvious ones, and (b) has a human judge the rest". If the organization has enabled GitHub secret scanning / push protection, that is anadditionalline of defense at push time, and does not replace this procedure (it does not work at local commit time).
Step 3: Determine the Conventional Commits type
| type | Purpose |
|---|---|
feat |
New feature |
fix |
Bug fix |
docs |
Documentation-only change |
refactor |
Code refactoring without functional change |
test |
Addition or modification of tests |
chore |
Changes to the build process, auxiliary tools, etc. |
style |
Code style-only changes (whitespace, formatting, etc.) |
build |
Changes to the build system or external dependencies |
ci |
Changes to CI configuration |
perf |
Performance improvement |
Step 4: Infer the scope
Infer the scope from the paths of the changed files. If it spans multiple areas, it may be omitted.
Step 5: Generate a commit message and confirm it with the user
Format: type(scope): subject
Example:
feat(auth): ソーシャルログイン機能を追加
fix(api): レスポンスのエラーハンドリングを修正
refactor(ui): コンポーネント構造を整理
Propose it to the user and get confirmation.
Step 6: Execute the commit
git commit -m "$(cat <<'EOF'
type(scope): subject
Co-Authored-By: Claude <noreply@anthropic.com>
EOF
)"
--no-verify Do not use it. The pre-commit hook is the last line of defense for secret scanning and linting, so if it fails, fix the cause and commit again.
Verification
After the commit is complete, verify with the following.
git log --oneline -3
- Check whether the latest commit message is in the
type(scope): subjectformat git show --stat HEADCheck whether the list of changed files is as intended with
Common failures
| Problem | Workaround |
|---|---|
| Mixing multiple concerns into one commit | Separate staging per concern with git add -p and make separate commits |
Choosing the wrong type (for bug fixes feat, for feature additions fix) |
Refer to the type table in Step 3, and choose giving priority to the intent of the diff |
| Skipping the secret check and proceeding to type determination | Do not omit Step 2. If detected, follow the classification in Step 2 (for 1, 2, and 4, abort; for 3, present the relevant line to the user and confirm) |
--no-verify Trying to bypass the hook with |
Investigate and fix the cause of the hook failure, then commit again (bypassing is prohibited) |
| The subject exceeds 72 characters | Omit the scope or shorten the subject. Write the details in the body |
Notes
- If there is a Breaking change, add
!:feat!: ...or in the bodyBREAKING CHANGE:describe - Write the subject in imperative and present tense (Japanese is acceptable)
- The subject must be within 72 characters
.envIf it contains secrets or credential files, warn and abort the commit (always detect this in Step 2)