git-for-each-ref
Iterate and format Git references (branches, tags)
TLDR
List all refs (branches and tags)
List only branches
List only tags
Show branches merged into HEAD
List short names of all refs
Sort refs by committer date (most recent first)
Sort refs by committer date (oldest first)
Limit output to a specified number of refs
SYNOPSIS
git for-each-ref [options...] [pattern...]
PARAMETERS
--format=<format>
Specify custom format string with placeholders like %(refname).
--sort=<key>[,key...]
Sort by key(s); - prefix for descending (e.g., -committerdate).
--count[=<n>]
Limit to first n results, adds legend header.
--no-legend
Suppress legend with --count.
--color[=<when>]
Enable auto/diff/always/never coloring.
--no-color
Disable all coloring.
--omit-empty
Skip refs where format yields empty output.
--shell
Quote output for safe shell eval.
--perl
Quote for Perl eval.
--python
Quote for Python eval.
--ruby
Quote for Ruby eval.
--tcl
Quote for Tcl eval.
--merged[=<object>]
Limit to refs merged into given object.
--no-merged[=<object>]
Exclude refs merged into object.
--points-at[=<object>]
Limit to refs pointing at object.
--contains[=<object>]
Limit to refs whose commits contain object.
--no-contains[=<object>]
Exclude refs containing object.
--rerere-byref
Only refs/rerere/ refs.
--glob=<glob>
Only refs matching glob pattern.
--references
Dereference symbolic refs.
--no-references
Don't dereference symrefs.
--heads
Only refs/heads refs.
--tags
Only refs/tags refs.
--remotes
Only refs/remotes refs.
--all
Scan refs/ and packed refs.
DESCRIPTION
git for-each-ref is a versatile Git porcelain command for listing and formatting repository references, including branches, tags, remote-tracking branches, and notes.
It processes each matching ref according to user-defined criteria, making it ideal for scripting, generating custom outputs, or querying ref properties without parsing git show-ref or similar.
By default, it prints the SHA-1 object name followed by the refname. Using --format, users specify placeholders like %(refname:short) for short branch names, %(subject) for commit subjects, or %(committerdate:format:%c) for formatted dates. Sorting via --sort orders output by keys such as committerdate (prefix - for descending). Filters like --merged, --contains, and --points-at select refs based on commit history relationships.
Quoting options (--shell, --perl, etc.) prepare output for safe evaluation in scripts. Color support enhances readability. Patterns limit scope, e.g., refs/heads/ for local branches.
This command powers many Git porcelain interfaces and is efficient for large repos due to packed ref support with --all. It's essential for advanced ref manipulation beyond basic listing tools.
CAVEATS
Format placeholders are extensive but version-specific; test in scripts. Large repos may slow without patterns/filters. History filters (--contains etc.) traverse commits, potentially expensive.
KEY FORMAT PLACEHOLDERS
%(refname): full name; %(refname:short): short; %(objectname:short): short SHA; %(committerdate:relative): relative date; %(subject): commit subject.
Full list via git for-each-ref --format='%(help)'.
PRACTICAL EXAMPLE
git for-each-ref --sort=-committerdate --format='%(color:yellow)%(refname:short)%(color:reset) %(color:green)%(committerdate:relative)%(color:reset)' refs/heads/
Sorted local branches with colors and relative dates.
HISTORY
Introduced in Git 1.5.4 (Feb 2007) as basic ref lister. Gained --format in 1.6.0 (2008), sorting/filters in 1.6.3+, quoting styles in 1.7+, colors in 1.7.2+. Continuously expanded for scripting power.
SEE ALSO
git-show-ref(1), git-branch(1), git-tag(1), git-ls-remote(1)


