The duct tape of any ref-related script you'll ever write.
Iterates refs and prints fields you choose with a format string. It is the duct tape of every ref-handling script you will ever write - and it replaces the fragile for ref in $(git branch) pattern.
A SELECT statement over refs/: pick atoms with %(...), filter by reachability or pointee, sort by any atom, cap with --count. Quoted, NUL-safe, and aware of peeled tags - everything ad-hoc shell loops get wrong.
git for-each-ref [--count=<count>] [--shell|--perl|--python|--tcl]
[(--sort=<key>)...] [--format=<format>]
[--include-root-refs] [ --stdin | <pattern>... ]
[--points-at=<object>]
[--merged[=<object>]] [--no-merged[=<object>]]
[--contains[=<object>]] [--no-contains[=<object>]]
[--exclude=<pattern>...]| Flag | What it does |
|---|---|
--format=<fmt> | Format string with atoms like %(refname), %(objectname), %(committerdate:iso), %(authoremail), %(upstream:short). |
--sort=<key> | Sort by an atom; prefix with - to reverse; repeat for multi-key sort (e.g. --sort=-committerdate). |
--count=<n> | Stop after the first n matches - cheap top-N once paired with --sort. |
--contains <commit> | Only refs whose tip has <commit> as ancestor; --no-contains for the inverse. |
--merged[=<commit>] | Only refs reachable from <commit> (default HEAD); --no-merged for unmerged ones. |
--points-at <object> | Only refs that point directly at <object> (peeled, so it works for tags). |
<pattern>... | Glob patterns like refs/heads/ or refs/tags/v; without any, all refs are listed. |
--contains a specific commit.--shell or NUL-terminated %00 in a custom format.git for-each-ref --sort=-committerdate --format='%(refname:short) %(committerdate:relative) %(authoremail)' refs/heads/git for-each-ref --sort=committerdate --count=10 --format='%(refname:short) %(committerdate:short)' refs/heads/git for-each-ref --contains <sha> --format='%(refname:short)' refs/tags/git for-each-ref --no-merged main --format='%(refname:short)' refs/heads/git branch or git tag - it is faster, quoting-safe, and supports filters those commands don't.--format='%(refname)%00%(objectname)' (NUL separator) when piping into xargs -0 or other parsers.--sort=-committerdate with --count=N for cheap leaderboards instead of sorting in shell.git branch and git tag strip the refs/heads/ and refs/tags/ prefixes; for-each-ref does not unless you ask via %(refname:short).--contains checks the ref's tip, not full history; for tags that means the tag's peeled commit.refs/notes/ and refs/remotes/ - usually noise. Always scope it.Hit each option, then Check answers. Score is recorded; Next is always open.