Skip to content

for-each-ref Git Command Guide

The git for-each-ref command is used to iterate over Git references (branches, tags, remotes, etc.) and output information about each one in a customizable format. It provides a powerful way to query and format reference information for scripting and automation.

Terminal window
git for-each-ref [--count=<count>] [--sort=<key>] [--format=<format>] [--points-at=<object>] [--merged[=<object>]] [--no-merged[=<object>]] [--contains[=<object>]] [--no-contains[=<object>]] [--exclude=<pattern>] [--stdin] [<pattern>...]
OptionDescription
—count=Limit number of refs shown
—sort=Sort by field (refname, subject, etc.)
—format=Custom output format with placeholders (e.g., %(refname))
—color[=]Color output
—shell/—perl/—python/—tclQuote output for specified language
—points-at=Only refs pointing to specified object
—merged[=]Only refs whose tips are reachable from commit
—no-merged[=]Only refs whose tips are not reachable
—contains[=]Only refs containing specified commit
—no-contains[=]Only refs not containing specified commit
—exclude=Exclude refs matching pattern
—ignore-caseCase insensitive sorting and filtering
—include-root-refsInclude HEAD and pseudorefs
—start-after=Paginate output starting after marker
—stdinRead patterns from stdin
ParameterDescription
Patterns to match reference names (e.g., refs/heads/*)
Terminal window
git for-each-ref --format='%(refname:short) %(subject)' refs/heads/

Show branch names and commit subjects.

Terminal window
git for-each-ref --format='%(authorname) %(authordate:relative) %(refname:short)' refs/heads/

Show author, relative date, and branch name.

Terminal window
git for-each-ref --sort=-authordate --format='%(authordate:relative) %(refname:short)' refs/heads/

Show branches sorted by recent author date.

Terminal window
git for-each-ref --contains HEAD~5 --format='%(refname:short)' refs/heads/

List branches containing a specific commit.

Terminal window
git for-each-ref --merged main --format='%(refname:short)' refs/heads/

Show branches already merged into main.

Terminal window
git for-each-ref --count=5 --format='%(refname)' | wc -l

Count total number of references (limiting to first 5).

Terminal window
git for-each-ref --shell --format='echo "Branch: %(refname)"' refs/heads/

Generate shell-quoted output.

Terminal window
git for-each-ref --points-at HEAD~10 --format='%(refname:short)' refs/heads/

Find branches pointing to a specific commit.

Terminal window
git for-each-ref --format='%(refname:short)' refs/remotes/origin/

List all remote tracking branches.

Terminal window
git for-each-ref --sort=-version:refname --format='%(refname:short)' refs/tags/

Sort tags in descending version order.

Terminal window
git for-each-ref --count=10 --start-after=refs/heads/main refs/heads/

Show 10 refs starting after a specific ref.

How do I list all branches with their last commit date?

Section titled “How do I list all branches with their last commit date?”

To show branches with dates, use:

Terminal window
git for-each-ref --format='%(refname:short) %(authordate:relative)' refs/heads/

To find branches merged into main, use:

Terminal window
git for-each-ref --merged main --format='%(refname:short)' refs/heads/

To sort references by date, use:

Terminal window
git for-each-ref --sort=-authordate refs/heads/

To generate shell-safe output, use:

Terminal window
git for-each-ref --shell --format='echo "%(refname)"' refs/heads/

How do I filter refs by commit containment?

Section titled “How do I filter refs by commit containment?”

To find refs containing a commit, use:

Terminal window
git for-each-ref --contains <commit> --format='%(refname)' refs/heads/

To exclude refs matching a pattern, use:

Terminal window
git for-each-ref --exclude='refs/heads/old-*' refs/heads/

The default format is: %(objectname) %(objecttype) %(refname)

To sort by reference name in reverse, use:

Terminal window
git for-each-ref --sort=-refname refs/tags/

How can I limit output to specific number?

Section titled “How can I limit output to specific number?”

To limit to first 10 refs, use:

Terminal window
git for-each-ref --count=10 refs/heads/

Applications of the git for-each-ref command

Section titled “Applications of the git for-each-ref command”
  1. Generating branch listings for automation scripts
  2. Creating custom Git status reports and dashboards
  3. Finding merged/unmerged branches for cleanup
  4. Analyzing repository structure and reference patterns
  5. Building Git-aware development tools and extensions