Synopsis
git for-each-ref [--format=<fmt>] [--sort=<key>] [<pattern>...]
Description
The git for-each-ref command iterates over every ref (branches, tags, remotes, notes, stash) and applies a customizable format. It is far more powerful than parsing git branch or git tag output: you can sort by version, by date, or by any field, and produce stable machine-readable output.
In day-to-day use, git for-each-ref integrates closely with shell aliases, editor plugins, and continuous integration. Power users often add aliases that combine flags they always pass, or wrap the command in scripts that enforce team conventions. Output formatting can be customized via Git config — pretty formats, color schemes, and pager behavior are all tunable. When something goes wrong, the first diagnostic step is usually to re-run the command with GIT_TRACE=1 in the environment, which reveals the underlying plumbing calls. For unusual situations, the --help output (git for-each-ref --help) opens the full manual page with details on every option, including those rarely used in casual workflows but essential for debugging or scripting at scale.
Understanding how git for-each-ref interacts with the rest of Git's data model — the object database, the index, refs, and the working tree — pays dividends. Each command operates on some subset of these pieces, and knowing which it touches helps predict outcomes and recover from mistakes. Reading the official Git documentation alongside hands-on practice in a throwaway repository is the fastest way to internalize the nuances. Most production issues with Git stem from one of three causes: surprising default behavior, partial network operations, or rewriting history that was already shared. A working mental model of git for-each-ref's side effects helps avoid all three.
When to Use
Reach for for-each-ref in scripts that need detailed ref metadata: build version selectors, dashboards of branch ages, automated cleanup of stale branches, or release-note generators.
Common Options
| Option | Description |
|---|---|
--format=<fmt> | Format string with field placeholders. |
--sort=<key> | Sort by field (prefix - for descending). |
--count=<n> | Limit number of entries. |
--contains <commit> | Refs containing the given commit. |
--merged <commit> | Refs merged into the given commit. |
--points-at <commit> | Refs that directly point at a commit. |
Examples
git for-each-ref --format='%(refname:short) %(objectname:short)' refs/heads/
# Local branch names with abbreviated SHAs
git for-each-ref --sort=-committerdate --count=10 \
--format='%(committerdate:short) %(refname:short)' refs/heads/
# Top 10 most recently updated branches
git for-each-ref --sort=-v:refname --format='%(refname:short)' refs/tags/ | head -5
# Five highest semver tags
Common Mistakes
Forgetting the trailing / on refs/heads/ patterns can match unintended refs. Format strings can be tricky — quote field names with parentheses. --sort=v:refname only works on tags that look like versions.
Related Commands
git branch, git tag, git ls-remote, git rev-parse