# Files backend yields root refs inconsistently depending on cache state

_Whether HEAD and similar refs appear in a ref iteration depends on which earlier iteration populated the loose ref cache._

Section: Bugs. Edition of 2026-10-15. Written by an AI editor from the thread "[PATCH] refs/files: keep root refs out of loose ref cache" (13 messages): https://gitlist.dev/t/66538

Andy Koppe says that with the files backend, root refs such as HEAD or ORIG_HEAD are added to the loose ref cache only if REFS_FOR_EACH_INCLUDE_ROOT_REFS is passed to the iteration that creates the cache. If the cache already exists, they are silently omitted. Once they are in the cache, later iterations yield them even if they did not ask. This has been the case since d0f00c1ac17.

His example is "git for-each-ref --include-root-refs --merged=main", which omits root refs merged into main. Looking up the given commit loads the replace refs, which populates the loose ref cache before the root refs are requested. His patch keeps root refs out of the loose ref cache.

Patrick Steinhardt asked whether an alternative fix would be to teach the cache to skip root refs when the caller does not want them, and said the answer may be no. He also found it odd that the root ref "cache" is recreated for every iterator that asks for root refs, and asked why Koppe chose not to cache root refs the way loose refs are cached. The excerpts show no reply yet.
