Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions .claude/hooks/git-commit-reminder.sh

This file was deleted.

3 changes: 1 addition & 2 deletions .claude/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"hooks": [
{
"type": "command",
"if": "Bash(git commit *)",
"command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/git-commit-reminder.sh",
"command": "${CLAUDE_PROJECT_DIR}/.claude/skills/git-why/.claude/hooks/git-commit-reminder.sh",
"args": []
}
]
Expand Down
69 changes: 69 additions & 0 deletions .claude/skills/git-why/.claude/hooks/git-commit-reminder.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env bash
# PostToolUse hook (matcher: Bash).
#
# PostToolUse only fires when the matched tool call succeeded -- a failing
# Bash command (e.g. a commit rejected by a pre-commit hook) fires
# PostToolUseFailure instead, which this hook is not registered on. So by the
# time this script runs, any commit in the command has already succeeded; no
# exit-code checking is needed here.
#
# "Was this a git commit" is decided here rather than by an `if` matcher on
# the settings entry. `Bash(git commit *)` needs `git` and `commit` to be
# adjacent, so it misses `git -C dir commit`, `git -c k=v commit` and
# `git --no-pager commit` (#2); loosening it to `Bash(git *commit*)` would
# trade that for false positives like `git log --grep=commit`. Instead, for
# each shell command segment, this skips leading VAR=value assignments,
# requires the word `git`, skips git's own options (and the values of the
# ones that take one), and checks that the subcommand is exactly `commit`.
#
# This script makes no judgment call about whether the commit deserves a
# note -- it only guarantees the reminder fires every time, deterministically.
# The decision of whether to actually write one stays with Claude, per
# SKILL.md's own skip criteria (mechanical changes don't need a note).

input=$(cat)

is_commit() {
local segment w i
local -a words
# One segment per ; & | or newline -- crude, but quoting-aware parsing
# isn't needed to find a command word at the start of a segment.
while IFS= read -r segment; do
read -ra words <<<"$segment"
i=0
while [ "$i" -lt "${#words[@]}" ] && [[ ${words[$i]} == [A-Za-z_]*=* ]]; do
i=$((i + 1))
done
[ "${words[$i]:-}" = git ] || continue
i=$((i + 1))
while [ "$i" -lt "${#words[@]}" ]; do
w=${words[$i]}
case $w in
-C | -c | --git-dir | --work-tree | --namespace | --exec-path | --config-env | --super-prefix)
i=$((i + 2)) ;;
-*)
i=$((i + 1)) ;;
*)
break ;;
esac
done
[ "${words[$i]:-}" = commit ] && return 0
done < <(tr ';&|' '\n\n\n' <<<"$1")
return 1
}

# Without jq the command can't be extracted reliably, so fail open (remind)
# rather than silently dropping the reminder this hook exists to guarantee.
if command -v jq >/dev/null 2>&1; then
cmd=$(jq -r '.tool_input.command // empty' <<<"$input" 2>/dev/null) || cmd=
is_commit "$cmd" || exit 0
fi

cat <<'JSON'
{
"hookSpecificOutput": {
"hookEventName": "PostToolUse",
"additionalContext": "A git commit just succeeded. Check it against the git-why skill's SKILL.md note criteria; skip if mechanical."
}
}
JSON
16 changes: 16 additions & 0 deletions .claude/skills/git-why/.claude/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"hooks": {
"PostToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/git-commit-reminder.sh",
"args": []
}
]
}
]
}
}
1 change: 1 addition & 0 deletions .claude/skills/git-why/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
21 changes: 21 additions & 0 deletions .claude/skills/git-why/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2026 Prateek Sharma

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
158 changes: 158 additions & 0 deletions .claude/skills/git-why/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# git-why

A Claude Code skill that captures the design reasoning behind non-trivial
commits as git notes attached to the commit itself. The "why" — rejected
alternatives, constraints that forced an approach, tradeoffs — becomes
retrievable from the terminal forever, independent of the chat that produced
it.

## Install

Clone this repo into `.claude/skills/git-why/` inside the target project,
then remove the nested `.git`:

```bash
git clone <this-repo-url> .claude/skills/git-why && rm -rf .claude/skills/git-why/.git
```

Keep the two commands joined with `&&`. If the clone fails (no network, or
`.claude/skills/git-why` already exists), `rm -rf` must not run: on an
existing clone it would delete that clone's history, including any
unpushed work in it.

The `rm -rf` step matters if the target project is itself a git repo (the
usual case): without it, `git add` there treats `.claude/skills/git-why` as
an embedded repository and stages a bare gitlink instead of the actual
files — SKILL.md and the scripts silently don't get committed, and the next
person to clone the target project gets an empty directory where the skill
should be, with no error at any point. Deleting `.git` first makes it plain
vendored files that commit normally.

The resulting path **must** be `.claude/skills/git-why/SKILL.md`. A common
way this silently fails to load is an extra level of nesting, e.g.
`.claude/skills/git-why/git-why/SKILL.md` — double check the clone didn't
create a nested directory.

### Updating

With `.git` removed there's nothing to `git pull`. To update, delete the
folder and install again:

```bash
rm -rf .claude/skills/git-why && git clone <this-repo-url> .claude/skills/git-why && rm -rf .claude/skills/git-why/.git
```

This replaces the folder wholesale, so any local edits to the skill are
lost. Review `git diff .claude/skills/git-why` in the target project before
committing the update.

### Optional: the commit-reminder hook

The skill's own `.claude/settings.json` is **not** read when it's installed
under `.claude/skills/` — Claude Code only loads a project's top-level
`.claude/settings.json`. To get the reminder after every commit in the
target project, add this to that file (merging into any existing `hooks`):

```json
{
"hooks": {
"PostToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "${CLAUDE_PROJECT_DIR}/.claude/skills/git-why/.claude/hooks/git-commit-reminder.sh"
}
]
}
]
}
}
```

Pointing at the vendored script, rather than copying it, means updating the
skill also updates the hook. Leave out an `if` condition: the script decides
for itself whether the command was a commit (see below), and
`"if": "Bash(git commit *)"` would hide commits like `git -C dir commit`
from it. The script needs `jq` to read the command; without it, it reminds
after every Bash call rather than never.

## How it works

Claude applies this skill proactively: right after a commit that involved
real design reasoning, and whenever you ask "why does this exist" about
existing code. This only happens when Claude is present in the session —
there's no hook or background automation by default, and no retroactive
notes for commits made outside Claude. The full criteria for what warrants a
note, how to write one, and where to put it when a decision spans two files
lives in [SKILL.md](SKILL.md) — that's the canonical source Claude actually
follows; this file just covers install and the lookup tools.

This repo also ships an optional `PostToolUse` hook
(`.claude/settings.json` and `.claude/hooks/git-commit-reminder.sh`), which
fires that reminder deterministically after every `git commit` instead of
relying on Claude to remember. It's active when this repo is the open
project; other projects opt in as described under
[Install](#optional-the-commit-reminder-hook). The script itself checks
whether a Bash command ran `git commit`, including forms like
`git -C dir commit`, `git -c key=value commit` and `git --no-pager commit`,
while ignoring look-alikes such as `git log --grep=commit` or
`git commit-tree`.

## Usage

### `scripts/git-why.sh <file> [line]`

Find the commit(s) that touched a file (or one line), and print any
reasoning note attached to them. Navigation-first: use it when you already
know the file.

```bash
scripts/git-why.sh internal/process/lock.go
scripts/git-why.sh internal/process/lock.go 41
```

### `scripts/git-notes-grep.sh <keyword> [more keywords...]`

Search every note's text for a keyword (case-insensitive, OR'd together,
matched literally). Recall-first: use it when you remember a concept but not
which file it lives in.

```bash
scripts/git-notes-grep.sh lock reboot
```

### `scripts/git-why-record.sh [path]`

Coverage check: report which recent commits don't have a reasoning note yet.
Purely mechanical, no AI — just how many commits have piled up since the
last note. It counts the tail only: un-noted commits older than the most
recent note aren't included, so it isn't a total-coverage number.

```bash
scripts/git-why-record.sh
scripts/git-why-record.sh internal/process/lock.go
```

## Caveat: notes don't travel with push/fetch/clone by default

`git notes` live on a separate ref, `refs/notes/commits`, which is **not**
included in a normal `git push`, `git fetch`, or `git clone`.

For a solo project this doesn't matter. But the moment a project is shared
or cloned onto another machine, notes must be explicitly pushed and fetched,
or they will silently not show up for a collaborator:

```bash
git push origin refs/notes/commits
git fetch origin refs/notes/commits:refs/notes/commits
```

This is the single most likely thing to confuse someone the first time
notes don't appear after a clone — if you're setting this up on a shared
repo, make sure everyone knows to run the fetch command above.

## License

MIT — see [LICENSE](LICENSE).
Loading
Loading