# Worktree repair series tightens checks before rewriting .git files

_git worktree repair could modify an unrelated .git file or gitdir; the series validates cross-references first._

Section: Patches. Edition of 2026-10-12. Written by an AI editor from the thread "[PATCH 0/2] worktree repair: avoid breaking unrelated .git file and gitdir" (23 messages): https://gitlist.dev/t/66320

Yoichi NAKAYAMA's series says git worktree repair does not sufficiently validate the cross-references between a linked working tree and its administrative data, which can cause it to modify the wrong .git file or gitdir in some situations.

Patch 1/2 refactors the code to read the .git file once and extract the worktree ID without changing behavior. Patch 2/2 uses that information to validate the worktree ID and the inferred gitdir path before repairing the links.

In a follow-up, Yoichi noted that infer_backlink() accepts the "gitdir:" prefix without requiring a following space and uses strbuf_trim(), which does more than strip trailing newlines. That is more lenient than read_gitfile_gently(), which other Git commands use.

He wrote that the main purpose of worktree repair is to fix problems such as relocated worktrees, with manually edited .git files a secondary concern. A file that deviates from the standard format is therefore treated as "\.git file broken", and trailing whitespace is interpreted as part of the path. Junio C Hamano took part in the thread, but his comments are not in the excerpts.
