git-for-each-ref
Iterate and format Git references (branches, tags)
SYNOPSIS
git for-each-ref [--shell] [--sort=
PARAMETERS
--shell
Format the output as shell commands assigning values to shell variables.
--sort=
Sort the output based on the specified key (e.g., 'refname', 'committerdate'). Use a '-' prefix to sort in reverse order.
--count=
Limit the output to the specified number of references.
--format=
Specify the format string for the output, using placeholders like '%(refname)', '%(objectname)', '%(committerdate)', etc.
[
A glob-like pattern to filter the references to be included (e.g., 'refs/heads/*', 'refs/tags/*').
DESCRIPTION
The `git for-each-ref` command is a powerful tool for iterating over Git references (branches, tags, remote tracking branches, etc.) and displaying them in a customizable format. It allows users to filter references based on various criteria and format the output using placeholders that represent reference properties. This command is highly useful for scripting, generating reports, or performing bulk operations on Git references.
It excels in generating customized output, making it suitable for tasks like listing branches based on last commit date, identifying stale branches, or creating custom reports about your Git repository. The format string uses placeholders to specify which properties of each reference to include in the output. For example, you could output the branch name, the committer date, and the commit subject for each branch in the repository. The command can be used with or without a filtering pattern, and allows for sorting and other advanced manipulation.
CAVEATS
The format string can become complex. Understanding the available placeholders is crucial for effective usage.
Improperly formatted strings may lead to errors, and performance could be affected in very large repositories.
PLACEHOLDERS
The `for-each-ref` command supports a variety of placeholders within the `--format` option. These placeholders allow users to access various properties of each reference, such as:
- %(refname): The full name of the reference (e.g., 'refs/heads/main').
- %(objectname): The object name (SHA-1 hash) pointed to by the reference.
- %(committerdate): The committer date of the commit pointed to by the reference.
- %(authorname), %(authoremail), %(authordate), %(committername), %(committeremail): Author and committer information.
- %(subject): The subject line of the commit message.
EXAMPLES
- List all branches sorted by last commit date:
git for-each-ref --sort='-committerdate' refs/heads
- List tags and their associated commit messages:
git for-each-ref --format='%(refname) %(subject)' refs/tags
- List remote tracking branches:
git for-each-ref refs/remotes
HISTORY
The `git for-each-ref` command was introduced to provide a more flexible and scriptable way to iterate over and display Git references than existing commands like `git branch` or `git tag`. It allows for customizing the output and filtering references based on various criteria, making it a valuable tool for automation and reporting.
SEE ALSO
git reflog(1), git branch(1), git tag(1)