Documentation / git-for-each-ref.txton commit Revert "color: check color.ui in git_default_config()" (33c643b)
   1git-for-each-ref(1)
   2===================
   3
   4NAME
   5----
   6git-for-each-ref - Output information on each ref
   7
   8SYNOPSIS
   9--------
  10[verse]
  11'git for-each-ref' [--count=<count>] [--shell|--perl|--python|--tcl]
  12                   [(--sort=<key>)...] [--format=<format>] [<pattern>...]
  13                   [--points-at <object>] [(--merged | --no-merged) [<object>]]
  14                   [--contains [<object>]] [--no-contains [<object>]]
  15
  16DESCRIPTION
  17-----------
  18
  19Iterate over all refs that match `<pattern>` and show them
  20according to the given `<format>`, after sorting them according
  21to the given set of `<key>`.  If `<count>` is given, stop after
  22showing that many refs.  The interpolated values in `<format>`
  23can optionally be quoted as string literals in the specified
  24host language allowing their direct evaluation in that language.
  25
  26OPTIONS
  27-------
  28<count>::
  29        By default the command shows all refs that match
  30        `<pattern>`.  This option makes it stop after showing
  31        that many refs.
  32
  33<key>::
  34        A field name to sort on.  Prefix `-` to sort in
  35        descending order of the value.  When unspecified,
  36        `refname` is used.  You may use the --sort=<key> option
  37        multiple times, in which case the last key becomes the primary
  38        key.
  39
  40<format>::
  41        A string that interpolates `%(fieldname)` from a ref being shown
  42        and the object it points at.  If `fieldname`
  43        is prefixed with an asterisk (`*`) and the ref points
  44        at a tag object, use the value for the field in the object
  45        which the tag object refers to (instead of the field in the tag object).
  46        When unspecified, `<format>` defaults to
  47        `%(objectname) SPC %(objecttype) TAB %(refname)`.
  48        It also interpolates `%%` to `%`, and `%xx` where `xx`
  49        are hex digits interpolates to character with hex code
  50        `xx`; for example `%00` interpolates to `\0` (NUL),
  51        `%09` to `\t` (TAB) and `%0a` to `\n` (LF).
  52
  53<pattern>...::
  54        If one or more patterns are given, only refs are shown that
  55        match against at least one pattern, either using fnmatch(3) or
  56        literally, in the latter case matching completely or from the
  57        beginning up to a slash.
  58
  59--color[=<when>]:
  60        Respect any colors specified in the `--format` option. The
  61        `<when>` field must be one of `always`, `never`, or `auto` (if
  62        `<when>` is absent, behave as if `always` was given).
  63
  64--shell::
  65--perl::
  66--python::
  67--tcl::
  68        If given, strings that substitute `%(fieldname)`
  69        placeholders are quoted as string literals suitable for
  70        the specified host language.  This is meant to produce
  71        a scriptlet that can directly be `eval`ed.
  72
  73--points-at <object>::
  74        Only list refs which points at the given object.
  75
  76--merged [<object>]::
  77        Only list refs whose tips are reachable from the
  78        specified commit (HEAD if not specified),
  79        incompatible with `--no-merged`.
  80
  81--no-merged [<object>]::
  82        Only list refs whose tips are not reachable from the
  83        specified commit (HEAD if not specified),
  84        incompatible with `--merged`.
  85
  86--contains [<object>]::
  87        Only list refs which contain the specified commit (HEAD if not
  88        specified).
  89
  90--no-contains [<object>]::
  91        Only list refs which don't contain the specified commit (HEAD
  92        if not specified).
  93
  94--ignore-case::
  95        Sorting and filtering refs are case insensitive.
  96
  97FIELD NAMES
  98-----------
  99
 100Various values from structured fields in referenced objects can
 101be used to interpolate into the resulting output, or as sort
 102keys.
 103
 104For all objects, the following names can be used:
 105
 106refname::
 107        The name of the ref (the part after $GIT_DIR/).
 108        For a non-ambiguous short name of the ref append `:short`.
 109        The option core.warnAmbiguousRefs is used to select the strict
 110        abbreviation mode. If `lstrip=<N>` (`rstrip=<N>`) is appended, strips `<N>`
 111        slash-separated path components from the front (back) of the refname
 112        (e.g. `%(refname:lstrip=2)` turns `refs/tags/foo` into `foo` and
 113        `%(refname:rstrip=2)` turns `refs/tags/foo` into `refs`).
 114        If `<N>` is a negative number, strip as many path components as
 115        necessary from the specified end to leave `-<N>` path components
 116        (e.g. `%(refname:lstrip=-2)` turns
 117        `refs/tags/foo` into `tags/foo` and `%(refname:rstrip=-1)`
 118        turns `refs/tags/foo` into `refs`). When the ref does not have
 119        enough components, the result becomes an empty string if
 120        stripping with positive <N>, or it becomes the full refname if
 121        stripping with negative <N>.  Neither is an error.
 122+
 123`strip` can be used as a synomym to `lstrip`.
 124
 125objecttype::
 126        The type of the object (`blob`, `tree`, `commit`, `tag`).
 127
 128objectsize::
 129        The size of the object (the same as 'git cat-file -s' reports).
 130
 131objectname::
 132        The object name (aka SHA-1).
 133        For a non-ambiguous abbreviation of the object name append `:short`.
 134        For an abbreviation of the object name with desired length append
 135        `:short=<length>`, where the minimum length is MINIMUM_ABBREV. The
 136        length may be exceeded to ensure unique object names.
 137
 138upstream::
 139        The name of a local ref which can be considered ``upstream''
 140        from the displayed ref. Respects `:short`, `:lstrip` and
 141        `:rstrip` in the same way as `refname` above.  Additionally
 142        respects `:track` to show "[ahead N, behind M]" and
 143        `:trackshort` to show the terse version: ">" (ahead), "<"
 144        (behind), "<>" (ahead and behind), or "=" (in sync). `:track`
 145        also prints "[gone]" whenever unknown upstream ref is
 146        encountered. Append `:track,nobracket` to show tracking
 147        information without brackets (i.e "ahead N, behind M").  Has
 148        no effect if the ref does not have tracking information
 149        associated with it.  All the options apart from `nobracket`
 150        are mutually exclusive, but if used together the last option
 151        is selected.
 152
 153push::
 154        The name of a local ref which represents the `@{push}`
 155        location for the displayed ref. Respects `:short`, `:lstrip`,
 156        `:rstrip`, `:track`, and `:trackshort` options as `upstream`
 157        does. Produces an empty string if no `@{push}` ref is
 158        configured.
 159
 160HEAD::
 161        '*' if HEAD matches current ref (the checked out branch), ' '
 162        otherwise.
 163
 164color::
 165        Change output color. Followed by `:<colorname>`, where color
 166        names are described under Values in the "CONFIGURATION FILE"
 167        section of linkgit:git-config[1].  For example,
 168        `%(color:bold red)`.
 169
 170align::
 171        Left-, middle-, or right-align the content between
 172        %(align:...) and %(end). The "align:" is followed by
 173        `width=<width>` and `position=<position>` in any order
 174        separated by a comma, where the `<position>` is either left,
 175        right or middle, default being left and `<width>` is the total
 176        length of the content with alignment. For brevity, the
 177        "width=" and/or "position=" prefixes may be omitted, and bare
 178        <width> and <position> used instead.  For instance,
 179        `%(align:<width>,<position>)`. If the contents length is more
 180        than the width then no alignment is performed. If used with
 181        `--quote` everything in between %(align:...) and %(end) is
 182        quoted, but if nested then only the topmost level performs
 183        quoting.
 184
 185if::
 186        Used as %(if)...%(then)...%(end) or
 187        %(if)...%(then)...%(else)...%(end).  If there is an atom with
 188        value or string literal after the %(if) then everything after
 189        the %(then) is printed, else if the %(else) atom is used, then
 190        everything after %(else) is printed. We ignore space when
 191        evaluating the string before %(then), this is useful when we
 192        use the %(HEAD) atom which prints either "*" or " " and we
 193        want to apply the 'if' condition only on the 'HEAD' ref.
 194        Append ":equals=<string>" or ":notequals=<string>" to compare
 195        the value between the %(if:...) and %(then) atoms with the
 196        given string.
 197
 198symref::
 199        The ref which the given symbolic ref refers to. If not a
 200        symbolic ref, nothing is printed. Respects the `:short`,
 201        `:lstrip` and `:rstrip` options in the same way as `refname`
 202        above.
 203
 204In addition to the above, for commit and tag objects, the header
 205field names (`tree`, `parent`, `object`, `type`, and `tag`) can
 206be used to specify the value in the header field.
 207
 208For commit and tag objects, the special `creatordate` and `creator`
 209fields will correspond to the appropriate date or name-email-date tuple
 210from the `committer` or `tagger` fields depending on the object type.
 211These are intended for working on a mix of annotated and lightweight tags.
 212
 213Fields that have name-email-date tuple as its value (`author`,
 214`committer`, and `tagger`) can be suffixed with `name`, `email`,
 215and `date` to extract the named component.
 216
 217The complete message in a commit and tag object is `contents`.
 218Its first line is `contents:subject`, where subject is the concatenation
 219of all lines of the commit message up to the first blank line.  The next
 220line is 'contents:body', where body is all of the lines after the first
 221blank line.  The optional GPG signature is `contents:signature`.  The
 222first `N` lines of the message is obtained using `contents:lines=N`.
 223Additionally, the trailers as interpreted by linkgit:git-interpret-trailers[1]
 224are obtained as 'contents:trailers'.
 225
 226For sorting purposes, fields with numeric values sort in numeric order
 227(`objectsize`, `authordate`, `committerdate`, `creatordate`, `taggerdate`).
 228All other fields are used to sort in their byte-value order.
 229
 230There is also an option to sort by versions, this can be done by using
 231the fieldname `version:refname` or its alias `v:refname`.
 232
 233In any case, a field name that refers to a field inapplicable to
 234the object referred by the ref does not cause an error.  It
 235returns an empty string instead.
 236
 237As a special case for the date-type fields, you may specify a format for
 238the date by adding `:` followed by date format name (see the
 239values the `--date` option to linkgit:git-rev-list[1] takes).
 240
 241Some atoms like %(align) and %(if) always require a matching %(end).
 242We call them "opening atoms" and sometimes denote them as %($open).
 243
 244When a scripting language specific quoting is in effect, everything
 245between a top-level opening atom and its matching %(end) is evaluated
 246according to the semantics of the opening atom and only its result
 247from the top-level is quoted.
 248
 249
 250EXAMPLES
 251--------
 252
 253An example directly producing formatted text.  Show the most recent
 2543 tagged commits:
 255
 256------------
 257#!/bin/sh
 258
 259git for-each-ref --count=3 --sort='-*authordate' \
 260--format='From: %(*authorname) %(*authoremail)
 261Subject: %(*subject)
 262Date: %(*authordate)
 263Ref: %(*refname)
 264
 265%(*body)
 266' 'refs/tags'
 267------------
 268
 269
 270A simple example showing the use of shell eval on the output,
 271demonstrating the use of --shell.  List the prefixes of all heads:
 272------------
 273#!/bin/sh
 274
 275git for-each-ref --shell --format="ref=%(refname)" refs/heads | \
 276while read entry
 277do
 278        eval "$entry"
 279        echo `dirname $ref`
 280done
 281------------
 282
 283
 284A bit more elaborate report on tags, demonstrating that the format
 285may be an entire script:
 286------------
 287#!/bin/sh
 288
 289fmt='
 290        r=%(refname)
 291        t=%(*objecttype)
 292        T=${r#refs/tags/}
 293
 294        o=%(*objectname)
 295        n=%(*authorname)
 296        e=%(*authoremail)
 297        s=%(*subject)
 298        d=%(*authordate)
 299        b=%(*body)
 300
 301        kind=Tag
 302        if test "z$t" = z
 303        then
 304                # could be a lightweight tag
 305                t=%(objecttype)
 306                kind="Lightweight tag"
 307                o=%(objectname)
 308                n=%(authorname)
 309                e=%(authoremail)
 310                s=%(subject)
 311                d=%(authordate)
 312                b=%(body)
 313        fi
 314        echo "$kind $T points at a $t object $o"
 315        if test "z$t" = zcommit
 316        then
 317                echo "The commit was authored by $n $e
 318at $d, and titled
 319
 320    $s
 321
 322Its message reads as:
 323"
 324                echo "$b" | sed -e "s/^/    /"
 325                echo
 326        fi
 327'
 328
 329eval=`git for-each-ref --shell --format="$fmt" \
 330        --sort='*objecttype' \
 331        --sort=-taggerdate \
 332        refs/tags`
 333eval "$eval"
 334------------
 335
 336
 337An example to show the usage of %(if)...%(then)...%(else)...%(end).
 338This prefixes the current branch with a star.
 339
 340------------
 341git for-each-ref --format="%(if)%(HEAD)%(then)* %(else)  %(end)%(refname:short)" refs/heads/
 342------------
 343
 344
 345An example to show the usage of %(if)...%(then)...%(end).
 346This prints the authorname, if present.
 347
 348------------
 349git for-each-ref --format="%(refname)%(if)%(authorname)%(then) Authored by: %(authorname)%(end)"
 350------------
 351
 352SEE ALSO
 353--------
 354linkgit:git-show-ref[1]
 355
 356GIT
 357---
 358Part of the linkgit:git[1] suite