# Git - Read-Only Queries and merge-tree Most review, release, and forensics questions do not need a checkout. Git plumbing reads objects at any ref, so answers stay fast and immune to stale-worktree errors. `git merge-tree` extends the same idea to merges: it computes the merged tree without touching HEAD, index, or worktree. ## Key points - Annotated tags are two-level objects. `git cat-file -p <tag-sha>` shows the `.object.sha` field, which gives the underlying commit. Direct pinning requires this dereference. - `git show <ref>:<path>`, `git diff <tag> <tag> -- <path>` and `git log <tag>..<tag> --oneline -- <path>` answer release comparisons without a checkout. - `git grep -F '<message>' <tagA> <tagB>` attributes a log pattern to its code site and diffs message sites between two tags. - `git log --all --oneline -- <file>` and `git log --all --oneline --grep='<PR number>'` give efficient forensic history for one file or a PR merge commit across all branches. - `git tag --contains <sha>` plus `git merge-base --is-ancestor <sha> <tag>` answers "which release contains this fix" without guessing. - `git range-diff <shaA> <shaB>` proves two commits carry the identical patch. Use it to confirm that a rebase replayed a commit with no duplicated content. - `git cat-file -e <sha>` exits 128 when the object is absent. It catches fabricated SHAs that hide behind otherwise verbatim quotes. - Verify a cherry-pick against its original by content diff. Ignore the `index` blob-hash line. It legitimately differs because of the different base. Everything else must match. - Word-boundary searches need `git grep -P '\b…'`. POSIX ERE has no `\b`, so an `-E` pattern with `\b` matches nothing and still exits 0. This is a false-pass evidence trap. - One call, `git rev-parse --is-inside-work-tree --show-toplevel --absolute-git-dir --git-common-dir`, answers four path questions in one process, about 10 ms. Seven to nine separate calls take about 138 ms. - `git rev-list --left-right --count HEAD...main` returning `0 4` means zero divergence. A clean fast-forward onto `main` is safe before you integrate work. - `git merge-tree --write-tree <base> <head>` computes the merged tree without touching HEAD, index or worktree. Anchor the probe to a freshly fetched upstream `main`, because a fork's `origin` lags. - Parse only `CONFLICT` lines. "Auto-merging" lines and binary-merge warnings are not conflicts. Misreading them demoted fully reviewable PRs whose only conflict was a generated binary artifact. - `git merge-tree --write-tree --name-only --no-messages -z <base> <head>` is a worktree-free conflict probe. Exit 0 means a clean merge. Exit 1 prints the tree OID and then NUL-separated conflicted paths. Any other exit is a probe error. - `git merge-tree` reads `.gitattributes` from the current checkout, not from the merged trees. A registered merge driver can hide the exact conflict the probe selects on. Write trusted `info/attributes` first and run two probes: with the driver disabled, the only conflict must be the artifact. With the driver enabled, the merge must be clean. - Distinguish a real source conflict from an artifact-only conflict badge before you ask an author to rebase. The distinction changes the review verdict. - Judge merged files against the merge-base, not against the PR head alone. An apparently "lost rules" merge result can be a deliberate removal on `main`. ## How to apply 1. Read a file at a ref: `git show <ref>:<path>`. 2. Compare two releases for one path: `git diff <tagA> <tagB> -- <path>` and `git log <tagA>..<tagB> --oneline -- <path>`. 3. Attribute a log pattern to code: `git grep -F '<message>' <tagA> <tagB>`. 4. Trace one file or a PR across branches: `git log --all --oneline -- <file>` and `git log --all --oneline --grep='<PR number>'`. 5. Find the release that contains a fix: `git tag --contains <sha>`, then `git merge-base --is-ancestor <sha> <tag>`. 6. Verify a cited SHA before you pin it: `git cat-file -e <sha>`. Exit 128 means the object is absent. 7. Check divergence before integration: `git rev-list --left-right --count HEAD...main`. A `0` on the left means a clean fast-forward. 8. Classify a PR conflict without a worktree: ```bash git fetch upstream main git merge-tree --write-tree --name-only --no-messages -z upstream/main <head> ``` Exit 0 means a clean merge. Exit 1 prints the tree OID, then NUL-separated conflicted paths. Treat any other exit as a probe error. 9. When a merge driver may apply, write trusted `info/attributes` first. Run the probe twice: with the driver disabled, the only conflict must be the generated artifact. With the driver enabled, the merge must be clean. ## Observations - [tooling] Annotated tags need a dereference before you pin the commit. #git (source: agent sessions, 2026-04) - [evidence] A word-boundary grep with `-E` matches nothing and still exits 0. Re-check with `-P` before you record a "no references" result. #evidence (source: agent sessions, 2026-10) - [verification] `git cat-file -e` catches fabricated SHAs that hide behind verbatim quotes. #verification (source: agent sessions, 2026-07) - [review] merge-tree noise, not real conflicts, demoted fully reviewable PRs. Parse only `CONFLICT` output. #code-review (source: agent sessions, 2026-07) - [review] A merge driver from the current checkout can hide the exact conflict a probe selects on. Run two probes with trusted `info/attributes`. #code-review (source: agent sessions, 2026-10) - [review] A "lost rules" merge result can be a deliberate removal on `main`. Judge against the merge-base. #code-review (source: agent sessions, 2026-10) - [performance] One `git rev-parse` call answers four path questions at a fraction of the cost of separate calls. #performance (source: agent sessions, 2026-10) - [verification] `git range-diff` proves a rebase replayed a commit with no duplicated content. #verification (source: agent sessions, 2026-09) ## Relations - relates_to [[resources/documentation/Git Configuration]] (query patterns assume a healthy base git setup) - relates_to [[resources/documentation/Git - Rebase, Stash, and Recovery]] (read-only queries avoid the worktree that these write-side flows rewrite) - relates_to [[resources/documentation/GitHub - Pull Request Workflow]] (merge-tree conflict classification feeds PR review verdicts) - relates_to [[resources/patterns/Verify Claims Against Primary Sources]] (cat-file -e and grep -P enforce the evidence rule) ## Sources - Personal agent-session notes, April 2026 to October 2026