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.
git for-each-ref Syntax:
Section titled “git for-each-ref Syntax:”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>...]Options:
Section titled “Options:”| Option | Description |
|---|---|
| —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/—tcl | Quote 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-case | Case insensitive sorting and filtering |
| —include-root-refs | Include HEAD and pseudorefs |
| —start-after= | Paginate output starting after marker |
| —stdin | Read patterns from stdin |
Parameters:
Section titled “Parameters:”| Parameter | Description |
|---|---|
| Patterns to match reference names (e.g., refs/heads/*) |
git for-each-ref Command Samples:
Section titled “git for-each-ref Command Samples:”List all branches with commit info
Section titled “List all branches with commit info”git for-each-ref --format='%(refname:short) %(subject)' refs/heads/Show branch names and commit subjects.
Custom format with author and date
Section titled “Custom format with author and date”git for-each-ref --format='%(authorname) %(authordate:relative) %(refname:short)' refs/heads/Show author, relative date, and branch name.
Sort branches by author date
Section titled “Sort branches by author date”git for-each-ref --sort=-authordate --format='%(authordate:relative) %(refname:short)' refs/heads/Show branches sorted by recent author date.
Filter branches that contain commit
Section titled “Filter branches that contain commit”git for-each-ref --contains HEAD~5 --format='%(refname:short)' refs/heads/List branches containing a specific commit.
Find merged and unmerged branches
Section titled “Find merged and unmerged branches”git for-each-ref --merged main --format='%(refname:short)' refs/heads/Show branches already merged into main.
Count total refs
Section titled “Count total refs”git for-each-ref --count=5 --format='%(refname)' | wc -lCount total number of references (limiting to first 5).
Generate shell script for branch deletion
Section titled “Generate shell script for branch deletion”git for-each-ref --shell --format='echo "Branch: %(refname)"' refs/heads/Generate shell-quoted output.
Find branches pointing to specific commit
Section titled “Find branches pointing to specific commit”git for-each-ref --points-at HEAD~10 --format='%(refname:short)' refs/heads/Find branches pointing to a specific commit.
Filter remote refs
Section titled “Filter remote refs”git for-each-ref --format='%(refname:short)' refs/remotes/origin/List all remote tracking branches.
Sort tags by version
Section titled “Sort tags by version”git for-each-ref --sort=-version:refname --format='%(refname:short)' refs/tags/Sort tags in descending version order.
Paginate large output
Section titled “Paginate large output”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:
git for-each-ref --format='%(refname:short) %(authordate:relative)' refs/heads/How can I find merged branches?
Section titled “How can I find merged branches?”To find branches merged into main, use:
git for-each-ref --merged main --format='%(refname:short)' refs/heads/How do I sort refs by date?
Section titled “How do I sort refs by date?”To sort references by date, use:
git for-each-ref --sort=-authordate refs/heads/How can I format output for scripting?
Section titled “How can I format output for scripting?”To generate shell-safe output, use:
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:
git for-each-ref --contains <commit> --format='%(refname)' refs/heads/How do I exclude certain refs?
Section titled “How do I exclude certain refs?”To exclude refs matching a pattern, use:
git for-each-ref --exclude='refs/heads/old-*' refs/heads/What’s the default output format?
Section titled “What’s the default output format?”The default format is: %(objectname) %(objecttype) %(refname)
How do I sort by reference name?
Section titled “How do I sort by reference name?”To sort by reference name in reverse, use:
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:
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”- Generating branch listings for automation scripts
- Creating custom Git status reports and dashboards
- Finding merged/unmerged branches for cleanup
- Analyzing repository structure and reference patterns
- Building Git-aware development tools and extensions