blob: 901faef1bfdcea991fe1b0a32ab5ecd22771967f [file] [log] [blame]
Junio C Hamanoff4b4312006-10-25 22:55:311git-for-each-ref(1)
2===================
3
4NAME
5----
6git-for-each-ref - Output information on each ref
7
8SYNOPSIS
9--------
Junio C Hamanoa9b8d242007-05-19 04:51:5510[verse]
Junio C Hamanofce7c7e2008-07-02 03:06:3811'git for-each-ref' [--count=<count>] [--shell|--perl|--python|--tcl]
Junio C Hamanod2179ef2010-10-22 04:12:1712 [(--sort=<key>)...] [--format=<format>] [<pattern>...]
Junio C Hamanod4bbec02017-09-25 07:24:5313 [--points-at=<object>]
14 (--merged[=<object>] | --no-merged[=<object>])
15 [--contains[=<object>]] [--no-contains[=<object>]]
Junio C Hamanoff4b4312006-10-25 22:55:3116
17DESCRIPTION
18-----------
19
20Iterate over all refs that match `<pattern>` and show them
21according to the given `<format>`, after sorting them according
Junio C Hamanoa2f46622008-09-03 05:20:3122to the given set of `<key>`. If `<count>` is given, stop after
Junio C Hamanof9771f62007-01-17 17:42:3023showing that many refs. The interpolated values in `<format>`
Junio C Hamanoff4b4312006-10-25 22:55:3124can optionally be quoted as string literals in the specified
25host language allowing their direct evaluation in that language.
26
27OPTIONS
28-------
Junio C Hamanod4bbec02017-09-25 07:24:5329<pattern>...::
30If one or more patterns are given, only refs are shown that
31match against at least one pattern, either using fnmatch(3) or
32literally, in the latter case matching completely or from the
33beginning up to a slash.
34
35--count=<count>::
Junio C Hamanoff4b4312006-10-25 22:55:3136By default the command shows all refs that match
37`<pattern>`. This option makes it stop after showing
38that many refs.
39
Junio C Hamanod4bbec02017-09-25 07:24:5340--sort=<key>::
Junio C Hamanoff4b4312006-10-25 22:55:3141A field name to sort on. Prefix `-` to sort in
42descending order of the value. When unspecified,
Junio C Hamanof7c042d2008-06-06 22:50:5343`refname` is used. You may use the --sort=<key> option
44multiple times, in which case the last key becomes the primary
45key.
Junio C Hamanoff4b4312006-10-25 22:55:3146
Junio C Hamanod4bbec02017-09-25 07:24:5347--format=<format>::
Junio C Hamanoa4ff7292017-08-23 21:56:4348A string that interpolates `%(fieldname)` from a ref being shown
49and the object it points at. If `fieldname`
Junio C Hamanoff4b4312006-10-25 22:55:3150is prefixed with an asterisk (`*`) and the ref points
Junio C Hamanoa4ff7292017-08-23 21:56:4351at a tag object, use the value for the field in the object
52which the tag object refers to (instead of the field in the tag object).
53When unspecified, `<format>` defaults to
Junio C Hamano4de43af2006-10-28 21:29:0154`%(objectname) SPC %(objecttype) TAB %(refname)`.
55It also interpolates `%%` to `%`, and `%xx` where `xx`
56are hex digits interpolates to character with hex code
57`xx`; for example `%00` interpolates to `\0` (NUL),
58`%09` to `\t` (TAB) and `%0a` to `\n` (LF).
Junio C Hamanoff4b4312006-10-25 22:55:3159
Junio C Hamanobfd91f42018-08-17 22:21:1660--color[=<when>]::
Junio C Hamanod8dc0b52017-10-07 07:47:1961Respect any colors specified in the `--format` option. The
62`<when>` field must be one of `always`, `never`, or `auto` (if
63`<when>` is absent, behave as if `always` was given).
64
Junio C Hamanoeb415992008-06-08 22:49:4765--shell::
66--perl::
67--python::
68--tcl::
Junio C Hamanoff4b4312006-10-25 22:55:3169If given, strings that substitute `%(fieldname)`
70placeholders are quoted as string literals suitable for
71the specified host language. This is meant to produce
72a scriptlet that can directly be `eval`ed.
73
Junio C Hamanod4bbec02017-09-25 07:24:5374--points-at=<object>::
Junio C Hamano1eb56092015-10-05 20:39:5375Only list refs which points at the given object.
76
Junio C Hamanod4bbec02017-09-25 07:24:5377--merged[=<object>]::
Junio C Hamano1eb56092015-10-05 20:39:5378Only list refs whose tips are reachable from the
Junio C Hamano59a5f532017-04-11 08:38:0279specified commit (HEAD if not specified),
80incompatible with `--no-merged`.
Junio C Hamano1eb56092015-10-05 20:39:5381
Junio C Hamanod4bbec02017-09-25 07:24:5382--no-merged[=<object>]::
Junio C Hamano1eb56092015-10-05 20:39:5383Only list refs whose tips are not reachable from the
Junio C Hamano59a5f532017-04-11 08:38:0284specified commit (HEAD if not specified),
85incompatible with `--merged`.
Junio C Hamano1eb56092015-10-05 20:39:5386
Junio C Hamanod4bbec02017-09-25 07:24:5387--contains[=<object>]::
Junio C Hamano5996b672016-04-13 22:01:4188Only list refs which contain the specified commit (HEAD if not
Junio C Hamano1eb56092015-10-05 20:39:5389specified).
Junio C Hamanoff4b4312006-10-25 22:55:3190
Junio C Hamanod4bbec02017-09-25 07:24:5391--no-contains[=<object>]::
Junio C Hamano59a5f532017-04-11 08:38:0292Only list refs which don't contain the specified commit (HEAD
93if not specified).
94
Junio C Hamano52b1cfb2016-12-20 00:18:3695--ignore-case::
96Sorting and filtering refs are case insensitive.
97
Junio C Hamanoff4b4312006-10-25 22:55:3198FIELD NAMES
99-----------
100
101Various values from structured fields in referenced objects can
102be used to interpolate into the resulting output, or as sort
103keys.
104
105For all objects, the following names can be used:
106
107refname::
Junio C Hamano42f855f2007-02-06 00:09:38108The name of the ref (the part after $GIT_DIR/).
Junio C Hamano5e4d8de2008-09-19 06:27:02109For a non-ambiguous short name of the ref append `:short`.
Junio C Hamano3bd66e92009-04-18 06:17:56110The option core.warnAmbiguousRefs is used to select the strict
Junio C Hamanocbe0eb22017-02-27 23:37:49111abbreviation mode. If `lstrip=<N>` (`rstrip=<N>`) is appended, strips `<N>`
112slash-separated path components from the front (back) of the refname
113(e.g. `%(refname:lstrip=2)` turns `refs/tags/foo` into `foo` and
114`%(refname:rstrip=2)` turns `refs/tags/foo` into `refs`).
115If `<N>` is a negative number, strip as many path components as
116necessary from the specified end to leave `-<N>` path components
117(e.g. `%(refname:lstrip=-2)` turns
118`refs/tags/foo` into `tags/foo` and `%(refname:rstrip=-1)`
119turns `refs/tags/foo` into `refs`). When the ref does not have
120enough components, the result becomes an empty string if
121stripping with positive <N>, or it becomes the full refname if
122stripping with negative <N>. Neither is an error.
123+
Junio C Hamano96153bf2018-04-25 08:25:34124`strip` can be used as a synonym to `lstrip`.
Junio C Hamanoff4b4312006-10-25 22:55:31125
126objecttype::
127The type of the object (`blob`, `tree`, `commit`, `tag`).
128
129objectsize::
Junio C Hamano1aa40d22010-01-21 17:46:43130The size of the object (the same as 'git cat-file -s' reports).
Junio C Hamanoff4b4312006-10-25 22:55:31131
132objectname::
133The object name (aka SHA-1).
Junio C Hamanoe97d5772010-05-19 08:13:58134For a non-ambiguous abbreviation of the object name append `:short`.
Junio C Hamanocbe0eb22017-02-27 23:37:49135For an abbreviation of the object name with desired length append
136`:short=<length>`, where the minimum length is MINIMUM_ABBREV. The
137length may be exceeded to ensure unique object names.
Junio C Hamanoff4b4312006-10-25 22:55:31138
Junio C Hamano98e32c32009-04-13 02:39:53139upstream::
140The name of a local ref which can be considered ``upstream''
Junio C Hamanocbe0eb22017-02-27 23:37:49141from the displayed ref. Respects `:short`, `:lstrip` and
142`:rstrip` in the same way as `refname` above. Additionally
143respects `:track` to show "[ahead N, behind M]" and
144`:trackshort` to show the terse version: ">" (ahead), "<"
145(behind), "<>" (ahead and behind), or "=" (in sync). `:track`
146also prints "[gone]" whenever unknown upstream ref is
147encountered. Append `:track,nobracket` to show tracking
Junio C Hamanob72f6032017-11-15 05:57:08148information without brackets (i.e "ahead N, behind M").
149+
150For any remote-tracking branch `%(upstream)`, `%(upstream:remotename)`
151and `%(upstream:remoteref)` refer to the name of the remote and the
152name of the tracked remote ref, respectively. In other words, the
153remote-tracking branch can be updated explicitly and individually by
154using the refspec `%(upstream:remoteref):%(upstream)` to fetch from
155`%(upstream:remotename)`.
156+
157Has no effect if the ref does not have tracking information associated
158with it. All the options apart from `nobracket` are mutually exclusive,
159but if used together the last option is selected.
Junio C Hamanof079f702013-12-06 23:43:18160
Junio C Hamano975f4db2015-06-05 20:28:29161push::
Junio C Hamanocbe0eb22017-02-27 23:37:49162The name of a local ref which represents the `@{push}`
163location for the displayed ref. Respects `:short`, `:lstrip`,
Junio C Hamanob72f6032017-11-15 05:57:08164`:rstrip`, `:track`, `:trackshort`, `:remotename`, and `:remoteref`
165options as `upstream` does. Produces an empty string if no `@{push}`
166ref is configured.
Junio C Hamano975f4db2015-06-05 20:28:29167
Junio C Hamanof079f702013-12-06 23:43:18168HEAD::
169'*' if HEAD matches current ref (the checked out branch), ' '
170otherwise.
171
172color::
Junio C Hamano4b2a6c12017-08-22 18:32:37173Change output color. Followed by `:<colorname>`, where color
174names are described under Values in the "CONFIGURATION FILE"
175section of linkgit:git-config[1]. For example,
176`%(color:bold red)`.
Junio C Hamano98e32c32009-04-13 02:39:53177
Junio C Hamano1eb56092015-10-05 20:39:53178align::
179Left-, middle-, or right-align the content between
Junio C Hamano76df4f72016-02-26 22:13:10180%(align:...) and %(end). The "align:" is followed by
181`width=<width>` and `position=<position>` in any order
182separated by a comma, where the `<position>` is either left,
183right or middle, default being left and `<width>` is the total
184length of the content with alignment. For brevity, the
185"width=" and/or "position=" prefixes may be omitted, and bare
186<width> and <position> used instead. For instance,
187`%(align:<width>,<position>)`. If the contents length is more
188than the width then no alignment is performed. If used with
Junio C Hamano92d80372016-07-13 22:00:05189`--quote` everything in between %(align:...) and %(end) is
Junio C Hamano76df4f72016-02-26 22:13:10190quoted, but if nested then only the topmost level performs
191quoting.
Junio C Hamano1eb56092015-10-05 20:39:53192
Junio C Hamanocbe0eb22017-02-27 23:37:49193if::
194Used as %(if)...%(then)...%(end) or
195%(if)...%(then)...%(else)...%(end). If there is an atom with
196value or string literal after the %(if) then everything after
197the %(then) is printed, else if the %(else) atom is used, then
198everything after %(else) is printed. We ignore space when
199evaluating the string before %(then), this is useful when we
200use the %(HEAD) atom which prints either "*" or " " and we
201want to apply the 'if' condition only on the 'HEAD' ref.
202Append ":equals=<string>" or ":notequals=<string>" to compare
203the value between the %(if:...) and %(then) atoms with the
204given string.
205
206symref::
207The ref which the given symbolic ref refers to. If not a
208symbolic ref, nothing is printed. Respects the `:short`,
209`:lstrip` and `:rstrip` options in the same way as `refname`
210above.
211
Junio C Hamanoff4b4312006-10-25 22:55:31212In addition to the above, for commit and tag objects, the header
213field names (`tree`, `parent`, `object`, `type`, and `tag`) can
214be used to specify the value in the header field.
215
Junio C Hamano4e6ba272016-01-20 23:23:19216For commit and tag objects, the special `creatordate` and `creator`
217fields will correspond to the appropriate date or name-email-date tuple
218from the `committer` or `tagger` fields depending on the object type.
219These are intended for working on a mix of annotated and lightweight tags.
220
Junio C Hamanoff4b4312006-10-25 22:55:31221Fields that have name-email-date tuple as its value (`author`,
222`committer`, and `tagger`) can be suffixed with `name`, `email`,
223and `date` to extract the named component.
224
Junio C Hamano8fb66e52011-10-05 20:59:51225The complete message in a commit and tag object is `contents`.
Junio C Hamano281fd392012-09-17 23:57:41226Its first line is `contents:subject`, where subject is the concatenation
227of all lines of the commit message up to the first blank line. The next
Junio C Hamano1171ab42017-10-11 06:33:37228line is `contents:body`, where body is all of the lines after the first
Junio C Hamano1eb56092015-10-05 20:39:53229blank line. The optional GPG signature is `contents:signature`. The
230first `N` lines of the message is obtained using `contents:lines=N`.
Junio C Hamano52b1cfb2016-12-20 00:18:36231Additionally, the trailers as interpreted by linkgit:git-interpret-trailers[1]
Junio C Hamano1171ab42017-10-11 06:33:37232are obtained as `trailers` (or by using the historical alias
233`contents:trailers`). Non-trailer lines from the trailer block can be omitted
234with `trailers:only`. Whitespace-continuations can be removed from trailers so
235that each trailer appears on a line by itself with its full content with
236`trailers:unfold`. Both can be used together as `trailers:unfold,only`.
Junio C Hamanoff4b4312006-10-25 22:55:31237
Junio C Hamano4e6ba272016-01-20 23:23:19238For sorting purposes, fields with numeric values sort in numeric order
239(`objectsize`, `authordate`, `committerdate`, `creatordate`, `taggerdate`).
Junio C Hamanoff4b4312006-10-25 22:55:31240All other fields are used to sort in their byte-value order.
241
Junio C Hamano1eb56092015-10-05 20:39:53242There is also an option to sort by versions, this can be done by using
243the fieldname `version:refname` or its alias `v:refname`.
244
Junio C Hamanoff4b4312006-10-25 22:55:31245In any case, a field name that refers to a field inapplicable to
246the object referred by the ref does not cause an error. It
247returns an empty string instead.
248
Junio C Hamano1b50ce92007-10-03 12:05:53249As a special case for the date-type fields, you may specify a format for
Junio C Hamano1eb56092015-10-05 20:39:53250the date by adding `:` followed by date format name (see the
Junio C Hamano198b1f12016-05-17 22:27:24251values the `--date` option to linkgit:git-rev-list[1] takes).
Junio C Hamano1b50ce92007-10-03 12:05:53252
Junio C Hamanocbe0eb22017-02-27 23:37:49253Some atoms like %(align) and %(if) always require a matching %(end).
254We call them "opening atoms" and sometimes denote them as %($open).
255
256When a scripting language specific quoting is in effect, everything
257between a top-level opening atom and its matching %(end) is evaluated
258according to the semantics of the opening atom and only its result
259from the top-level is quoted.
260
Junio C Hamanoff4b4312006-10-25 22:55:31261
262EXAMPLES
263--------
264
265An example directly producing formatted text. Show the most recent
Junio C Hamanoa8cc1d82011-03-10 01:18:122663 tagged commits:
Junio C Hamanoff4b4312006-10-25 22:55:31267
268------------
269#!/bin/sh
270
Junio C Hamanofce7c7e2008-07-02 03:06:38271git for-each-ref --count=3 --sort='-*authordate' \
Junio C Hamanoff4b4312006-10-25 22:55:31272--format='From: %(*authorname) %(*authoremail)
273Subject: %(*subject)
274Date: %(*authordate)
275Ref: %(*refname)
276
277%(*body)
278' 'refs/tags'
279------------
280
281
282A simple example showing the use of shell eval on the output,
Junio C Hamanoa8cc1d82011-03-10 01:18:12283demonstrating the use of --shell. List the prefixes of all heads:
Junio C Hamanoff4b4312006-10-25 22:55:31284------------
285#!/bin/sh
286
Junio C Hamanofce7c7e2008-07-02 03:06:38287git for-each-ref --shell --format="ref=%(refname)" refs/heads | \
Junio C Hamanoff4b4312006-10-25 22:55:31288while read entry
289do
290eval "$entry"
291echo `dirname $ref`
292done
293------------
294
295
296A bit more elaborate report on tags, demonstrating that the format
Junio C Hamanoa8cc1d82011-03-10 01:18:12297may be an entire script:
Junio C Hamanoff4b4312006-10-25 22:55:31298------------
299#!/bin/sh
300
301fmt='
302r=%(refname)
303t=%(*objecttype)
304T=${r#refs/tags/}
305
306o=%(*objectname)
307n=%(*authorname)
308e=%(*authoremail)
309s=%(*subject)
310d=%(*authordate)
311b=%(*body)
312
313kind=Tag
314if test "z$t" = z
315then
316# could be a lightweight tag
317t=%(objecttype)
318kind="Lightweight tag"
319o=%(objectname)
320n=%(authorname)
321e=%(authoremail)
322s=%(subject)
323d=%(authordate)
324b=%(body)
325fi
326echo "$kind $T points at a $t object $o"
327if test "z$t" = zcommit
328then
329echo "The commit was authored by $n $e
330at $d, and titled
331
332 $s
333
334Its message reads as:
335"
336echo "$b" | sed -e "s/^/ /"
337echo
338fi
339'
340
Junio C Hamanofce7c7e2008-07-02 03:06:38341eval=`git for-each-ref --shell --format="$fmt" \
Junio C Hamanoff4b4312006-10-25 22:55:31342--sort='*objecttype' \
343--sort=-taggerdate \
344refs/tags`
345eval "$eval"
346------------
Junio C Hamanoa8cc1d82011-03-10 01:18:12347
Junio C Hamanocbe0eb22017-02-27 23:37:49348
349An example to show the usage of %(if)...%(then)...%(else)...%(end).
350This prefixes the current branch with a star.
351
352------------
353git for-each-ref --format="%(if)%(HEAD)%(then)* %(else) %(end)%(refname:short)" refs/heads/
354------------
355
356
357An example to show the usage of %(if)...%(then)...%(end).
358This prints the authorname, if present.
359
360------------
361git for-each-ref --format="%(refname)%(if)%(authorname)%(then) Authored by: %(authorname)%(end)"
362------------
363
Junio C Hamanofd98be82014-01-22 22:45:38364SEE ALSO
365--------
366linkgit:git-show-ref[1]
367
Junio C Hamanoa8cc1d82011-03-10 01:18:12368GIT
369---
370Part of the linkgit:git[1] suite