]> git.ipfire.org Git - thirdparty/git.git/blame - Documentation/git-for-each-ref.txt
Merge branch 'vd/glossary-dereference-peel'
[thirdparty/git.git] / Documentation / git-for-each-ref.txt
CommitLineData
9f613ddd
JH
1git-for-each-ref(1)
2===================
3
4NAME
5----
6git-for-each-ref - Output information on each ref
7
8SYNOPSIS
9--------
97925fde 10[verse]
b1889c36 11'git for-each-ref' [--count=<count>] [--shell|--perl|--python|--tcl]
b73dec55
DS
12 [(--sort=<key>)...] [--format=<format>]
13 [ --stdin | <pattern>... ]
3233d51d 14 [--points-at=<object>]
21bf9339 15 [--merged[=<object>]] [--no-merged[=<object>]]
3233d51d 16 [--contains[=<object>]] [--no-contains[=<object>]]
8255dd8a 17 [--exclude=<pattern> ...]
9f613ddd
JH
18
19DESCRIPTION
20-----------
21
22Iterate over all refs that match `<pattern>` and show them
23according to the given `<format>`, after sorting them according
d4040e0a 24to the given set of `<key>`. If `<count>` is given, stop after
23bfbb81 25showing that many refs. The interpolated values in `<format>`
9f613ddd 26can optionally be quoted as string literals in the specified
1729fa98 27host language allowing their direct evaluation in that language.
9f613ddd
JH
28
29OPTIONS
30-------
f7a32dd9
KD
31<pattern>...::
32 If one or more patterns are given, only refs are shown that
33 match against at least one pattern, either using fnmatch(3) or
34 literally, in the latter case matching completely or from the
35 beginning up to a slash.
36
b73dec55
DS
37--stdin::
38 If `--stdin` is supplied, then the list of patterns is read from
39 standard input instead of from the argument list.
40
f7a32dd9 41--count=<count>::
9f613ddd
JH
42 By default the command shows all refs that match
43 `<pattern>`. This option makes it stop after showing
44 that many refs.
45
f7a32dd9 46--sort=<key>::
9f613ddd
JH
47 A field name to sort on. Prefix `-` to sort in
48 descending order of the value. When unspecified,
c0f6dc9b
LW
49 `refname` is used. You may use the --sort=<key> option
50 multiple times, in which case the last key becomes the primary
51 key.
9f613ddd 52
f7a32dd9 53--format=<format>::
e4933cee
MG
54 A string that interpolates `%(fieldname)` from a ref being shown
55 and the object it points at. If `fieldname`
9f613ddd 56 is prefixed with an asterisk (`*`) and the ref points
794b7e16
MG
57 at a tag object, use the value for the field in the object
58 which the tag object refers to (instead of the field in the tag object).
59 When unspecified, `<format>` defaults to
ba7545ad
JN
60 `%(objectname) SPC %(objecttype) TAB %(refname)`.
61 It also interpolates `%%` to `%`, and `%xx` where `xx`
62 are hex digits interpolates to character with hex code
63 `xx`; for example `%00` interpolates to `\0` (NUL),
64 `%09` to `\t` (TAB) and `%0a` to `\n` (LF).
9f613ddd 65
dd61cc1c 66--color[=<when>]::
0c88bf50
JK
67 Respect any colors specified in the `--format` option. The
68 `<when>` field must be one of `always`, `never`, or `auto` (if
69 `<when>` is absent, behave as if `always` was given).
70
3240240f
SB
71--shell::
72--perl::
73--python::
74--tcl::
9f613ddd
JH
75 If given, strings that substitute `%(fieldname)`
76 placeholders are quoted as string literals suitable for
77 the specified host language. This is meant to produce
78 a scriptlet that can directly be `eval`ed.
79
3233d51d 80--points-at=<object>::
d325406e 81 Only list refs which points at the given object.
9f613ddd 82
3233d51d 83--merged[=<object>]::
7c328348 84 Only list refs whose tips are reachable from the
21bf9339 85 specified commit (HEAD if not specified).
7c328348 86
3233d51d 87--no-merged[=<object>]::
7c328348 88 Only list refs whose tips are not reachable from the
21bf9339 89 specified commit (HEAD if not specified).
7c328348 90
3233d51d 91--contains[=<object>]::
8b5a3e98 92 Only list refs which contain the specified commit (HEAD if not
4a71109a
KN
93 specified).
94
3233d51d 95--no-contains[=<object>]::
ac3f5a34
ÆAB
96 Only list refs which don't contain the specified commit (HEAD
97 if not specified).
98
3bb16a8b
NTND
99--ignore-case::
100 Sorting and filtering refs are case insensitive.
101
aabfdc95
ØW
102--omit-empty::
103 Do not print a newline after formatted refs where the format expands
104 to the empty string.
105
8255dd8a
TB
106--exclude=<pattern>::
107 If one or more patterns are given, only refs which do not match
108 any excluded pattern(s) are shown. Matching is done using the
109 same rules as `<pattern>` above.
110
9f613ddd
JH
111FIELD NAMES
112-----------
113
114Various values from structured fields in referenced objects can
115be used to interpolate into the resulting output, or as sort
116keys.
117
118For all objects, the following names can be used:
119
120refname::
69057cf3 121 The name of the ref (the part after $GIT_DIR/).
7d66f21a 122 For a non-ambiguous short name of the ref append `:short`.
2bb98169 123 The option core.warnAmbiguousRefs is used to select the strict
1a34728e
KN
124 abbreviation mode. If `lstrip=<N>` (`rstrip=<N>`) is appended, strips `<N>`
125 slash-separated path components from the front (back) of the refname
126 (e.g. `%(refname:lstrip=2)` turns `refs/tags/foo` into `foo` and
127 `%(refname:rstrip=2)` turns `refs/tags/foo` into `refs`).
1a0ca5e3 128 If `<N>` is a negative number, strip as many path components as
1a34728e 129 necessary from the specified end to leave `-<N>` path components
1a0ca5e3 130 (e.g. `%(refname:lstrip=-2)` turns
1a34728e
KN
131 `refs/tags/foo` into `tags/foo` and `%(refname:rstrip=-1)`
132 turns `refs/tags/foo` into `refs`). When the ref does not have
1a0ca5e3
KN
133 enough components, the result becomes an empty string if
134 stripping with positive <N>, or it becomes the full refname if
135 stripping with negative <N>. Neither is an error.
44a6b6ce 136+
c30d4f1b 137`strip` can be used as a synonym to `lstrip`.
9f613ddd
JH
138
139objecttype::
140 The type of the object (`blob`, `tree`, `commit`, `tag`).
141
142objectsize::
0b444cdb 143 The size of the object (the same as 'git cat-file -s' reports).
59012fe5
OT
144 Append `:disk` to get the size, in bytes, that the object takes up on
145 disk. See the note about on-disk sizes in the `CAVEATS` section below.
9f613ddd
JH
146objectname::
147 The object name (aka SHA-1).
67687fea 148 For a non-ambiguous abbreviation of the object name append `:short`.
42d0eb05
KN
149 For an abbreviation of the object name with desired length append
150 `:short=<length>`, where the minimum length is MINIMUM_ABBREV. The
151 length may be exceeded to ensure unique object names.
59012fe5
OT
152deltabase::
153 This expands to the object name of the delta base for the
154 given object, if it is stored as a delta. Otherwise it
155 expands to the null object name (all zeroes).
9f613ddd 156
8cae19d9
JK
157upstream::
158 The name of a local ref which can be considered ``upstream''
1a34728e
KN
159 from the displayed ref. Respects `:short`, `:lstrip` and
160 `:rstrip` in the same way as `refname` above. Additionally
161 respects `:track` to show "[ahead N, behind M]" and
162 `:trackshort` to show the terse version: ">" (ahead), "<"
163 (behind), "<>" (ahead and behind), or "=" (in sync). `:track`
164 also prints "[gone]" whenever unknown upstream ref is
165 encountered. Append `:track,nobracket` to show tracking
cc72385f
JS
166 information without brackets (i.e "ahead N, behind M").
167+
9700fae5
W
168For any remote-tracking branch `%(upstream)`, `%(upstream:remotename)`
169and `%(upstream:remoteref)` refer to the name of the remote and the
170name of the tracked remote ref, respectively. In other words, the
171remote-tracking branch can be updated explicitly and individually by
172using the refspec `%(upstream:remoteref):%(upstream)` to fetch from
173`%(upstream:remotename)`.
cc72385f
JS
174+
175Has no effect if the ref does not have tracking information associated
176with it. All the options apart from `nobracket` are mutually exclusive,
177but if used together the last option is selected.
8cae19d9 178
29bc8850 179push::
3ba308cb 180 The name of a local ref which represents the `@{push}`
17938f17 181 location for the displayed ref. Respects `:short`, `:lstrip`,
9700fae5
W
182 `:rstrip`, `:track`, `:trackshort`, `:remotename`, and `:remoteref`
183 options as `upstream` does. Produces an empty string if no `@{push}`
184 ref is configured.
29bc8850 185
7a48b832
RR
186HEAD::
187 '*' if HEAD matches current ref (the checked out branch), ' '
188 otherwise.
189
fddb74c9 190color::
5d3d0681
JK
191 Change output color. Followed by `:<colorname>`, where color
192 names are described under Values in the "CONFIGURATION FILE"
193 section of linkgit:git-config[1]. For example,
194 `%(color:bold red)`.
fddb74c9 195
ce592082
KN
196align::
197 Left-, middle-, or right-align the content between
395fb8f9
KN
198 %(align:...) and %(end). The "align:" is followed by
199 `width=<width>` and `position=<position>` in any order
200 separated by a comma, where the `<position>` is either left,
201 right or middle, default being left and `<width>` is the total
202 length of the content with alignment. For brevity, the
203 "width=" and/or "position=" prefixes may be omitted, and bare
204 <width> and <position> used instead. For instance,
205 `%(align:<width>,<position>)`. If the contents length is more
206 than the width then no alignment is performed. If used with
bcf9626a 207 `--quote` everything in between %(align:...) and %(end) is
395fb8f9
KN
208 quoted, but if nested then only the topmost level performs
209 quoting.
ce592082 210
c58492d4
KN
211if::
212 Used as %(if)...%(then)...%(end) or
213 %(if)...%(then)...%(else)...%(end). If there is an atom with
214 value or string literal after the %(if) then everything after
215 the %(then) is printed, else if the %(else) atom is used, then
216 everything after %(else) is printed. We ignore space when
217 evaluating the string before %(then), this is useful when we
218 use the %(HEAD) atom which prints either "*" or " " and we
219 want to apply the 'if' condition only on the 'HEAD' ref.
4f3e3b37
KN
220 Append ":equals=<string>" or ":notequals=<string>" to compare
221 the value between the %(if:...) and %(then) atoms with the
222 given string.
c58492d4 223
a7984101
KN
224symref::
225 The ref which the given symbolic ref refers to. If not a
1a34728e
KN
226 symbolic ref, nothing is printed. Respects the `:short`,
227 `:lstrip` and `:rstrip` options in the same way as `refname`
228 above.
a7984101 229
26c9c03f
KS
230signature::
231 The GPG signature of a commit.
232
233signature:grade::
234 Show "G" for a good (valid) signature, "B" for a bad
235 signature, "U" for a good signature with unknown validity, "X"
236 for a good signature that has expired, "Y" for a good
237 signature made by an expired key, "R" for a good signature
238 made by a revoked key, "E" if the signature cannot be
239 checked (e.g. missing key) and "N" for no signature.
240
241signature:signer::
242 The signer of the GPG signature of a commit.
243
244signature:key::
245 The key of the GPG signature of a commit.
246
247signature:fingerprint::
248 The fingerprint of the GPG signature of a commit.
249
250signature:primarykeyfingerprint::
251 The primary key fingerprint of the GPG signature of a commit.
252
253signature:trustlevel::
254 The trust level of the GPG signature of a commit. Possible
255 outputs are `ultimate`, `fully`, `marginal`, `never` and `undefined`.
256
2582083f
NB
257worktreepath::
258 The absolute path to the worktree in which the ref is checked
259 out, if it is checked out in any linked worktree. Empty string
260 otherwise.
261
49abcd21
DS
262ahead-behind:<committish>::
263 Two integers, separated by a space, demonstrating the number of
264 commits ahead and behind, respectively, when comparing the output
265 ref to the `<committish>` specified in the format.
266
f5d18f8c
KS
267describe[:options]::
268 A human-readable name, like linkgit:git-describe[1];
269 empty string for undescribable commits. The `describe` string may
270 be followed by a colon and one or more comma-separated options.
271+
272--
273tags=<bool-value>;;
274 Instead of only considering annotated tags, consider
275 lightweight tags as well; see the corresponding option in
276 linkgit:git-describe[1] for details.
277abbrev=<number>;;
278 Use at least <number> hexadecimal digits; see the corresponding
279 option in linkgit:git-describe[1] for details.
280match=<pattern>;;
281 Only consider tags matching the given `glob(7)` pattern,
282 excluding the "refs/tags/" prefix; see the corresponding option
283 in linkgit:git-describe[1] for details.
284exclude=<pattern>;;
285 Do not consider tags matching the given `glob(7)` pattern,
286 excluding the "refs/tags/" prefix; see the corresponding option
287 in linkgit:git-describe[1] for details.
288--
289
9f613ddd
JH
290In addition to the above, for commit and tag objects, the header
291field names (`tree`, `parent`, `object`, `type`, and `tag`) can
292be used to specify the value in the header field.
26bc0aaf 293Fields `tree` and `parent` can also be used with modifier `:short` and
837adb10 294`:short=<length>` just like `objectname`.
9f613ddd 295
e914ef0d
EW
296For commit and tag objects, the special `creatordate` and `creator`
297fields will correspond to the appropriate date or name-email-date tuple
298from the `committer` or `tagger` fields depending on the object type.
299These are intended for working on a mix of annotated and lightweight tags.
300
9f613ddd
JH
301Fields that have name-email-date tuple as its value (`author`,
302`committer`, and `tagger`) can be suffixed with `name`, `email`,
b82445dc
HV
303and `date` to extract the named component. For email fields (`authoremail`,
304`committeremail` and `taggeremail`), `:trim` can be appended to get the email
305without angle brackets, and `:localpart` to get the part before the `@` symbol
a3d2e83a
KS
306out of the trimmed email. In addition to these, the `:mailmap` option and the
307corresponding `:mailmap,trim` and `:mailmap,localpart` can be used (order does
308not matter) to get values of the name and email according to the .mailmap file
309or according to the file set in the mailmap.file or mailmap.blob configuration
310variable (see linkgit:gitmailmap[5]).
9f613ddd 311
bd0708c7
ZH
312The raw data in an object is `raw`.
313
314raw:size::
315 The raw data size of the object.
316
317Note that `--format=%(raw)` can not be used with `--python`, `--shell`, `--tcl`,
7121c4d4
ZH
318because such language may not support arbitrary binary data in their string
319variable type.
bd0708c7 320
9fcc9caf
CC
321The message in a commit or a tag object is `contents`, from which
322`contents:<part>` can be used to extract various parts out of:
323
b6839fda
CC
324contents:size::
325 The size in bytes of the commit or tag message.
326
9fcc9caf
CC
327contents:subject::
328 The first paragraph of the message, which typically is a
329 single line, is taken as the "subject" of the commit or the
330 tag message.
905f0a4e
HV
331 Instead of `contents:subject`, field `subject` can also be used to
332 obtain same results. `:sanitize` can be appended to `subject` for
333 subject line suitable for filename.
9fcc9caf
CC
334
335contents:body::
336 The remainder of the commit or the tag message that follows
337 the "subject".
338
339contents:signature::
340 The optional GPG signature of the tag.
341
342contents:lines=N::
343 The first `N` lines of the message.
344
b1d31c89 345Additionally, the trailers as interpreted by linkgit:git-interpret-trailers[1]
ee82a487
HV
346are obtained as `trailers[:options]` (or by using the historical alias
347`contents:trailers[:options]`). For valid [:option] values see `trailers`
348section of linkgit:git-log[1].
9f613ddd 349
e914ef0d
EW
350For sorting purposes, fields with numeric values sort in numeric order
351(`objectsize`, `authordate`, `committerdate`, `creatordate`, `taggerdate`).
9f613ddd
JH
352All other fields are used to sort in their byte-value order.
353
90c00408
KN
354There is also an option to sort by versions, this can be done by using
355the fieldname `version:refname` or its alias `v:refname`.
356
9f613ddd
JH
357In any case, a field name that refers to a field inapplicable to
358the object referred by the ref does not cause an error. It
359returns an empty string instead.
360
d392e712 361As a special case for the date-type fields, you may specify a format for
8f50d263 362the date by adding `:` followed by date format name (see the
1cca17df 363values the `--date` option to linkgit:git-rev-list[1] takes).
d392e712 364
c58492d4
KN
365Some atoms like %(align) and %(if) always require a matching %(end).
366We call them "opening atoms" and sometimes denote them as %($open).
367
368When a scripting language specific quoting is in effect, everything
369between a top-level opening atom and its matching %(end) is evaluated
370according to the semantics of the opening atom and only its result
371from the top-level is quoted.
372
9f613ddd
JH
373
374EXAMPLES
375--------
376
1729fa98 377An example directly producing formatted text. Show the most recent
22817b40 3783 tagged commits:
9f613ddd
JH
379
380------------
381#!/bin/sh
382
b1889c36 383git for-each-ref --count=3 --sort='-*authordate' \
9f613ddd
JH
384--format='From: %(*authorname) %(*authoremail)
385Subject: %(*subject)
386Date: %(*authordate)
387Ref: %(*refname)
388
389%(*body)
390' 'refs/tags'
391------------
392
1729fa98
AW
393
394A simple example showing the use of shell eval on the output,
22817b40 395demonstrating the use of --shell. List the prefixes of all heads:
1729fa98
AW
396------------
397#!/bin/sh
398
b1889c36 399git for-each-ref --shell --format="ref=%(refname)" refs/heads | \
1729fa98
AW
400while read entry
401do
402 eval "$entry"
403 echo `dirname $ref`
404done
405------------
406
407
408A bit more elaborate report on tags, demonstrating that the format
22817b40 409may be an entire script:
9f613ddd
JH
410------------
411#!/bin/sh
412
413fmt='
414 r=%(refname)
415 t=%(*objecttype)
416 T=${r#refs/tags/}
417
418 o=%(*objectname)
419 n=%(*authorname)
420 e=%(*authoremail)
421 s=%(*subject)
422 d=%(*authordate)
423 b=%(*body)
424
425 kind=Tag
426 if test "z$t" = z
427 then
428 # could be a lightweight tag
429 t=%(objecttype)
430 kind="Lightweight tag"
431 o=%(objectname)
432 n=%(authorname)
433 e=%(authoremail)
434 s=%(subject)
435 d=%(authordate)
436 b=%(body)
437 fi
438 echo "$kind $T points at a $t object $o"
439 if test "z$t" = zcommit
440 then
441 echo "The commit was authored by $n $e
442at $d, and titled
443
444 $s
445
446Its message reads as:
447"
448 echo "$b" | sed -e "s/^/ /"
449 echo
450 fi
451'
452
b1889c36 453eval=`git for-each-ref --shell --format="$fmt" \
9f613ddd
JH
454 --sort='*objecttype' \
455 --sort=-taggerdate \
456 refs/tags`
457eval "$eval"
458------------
621c39de 459
c58492d4
KN
460
461An example to show the usage of %(if)...%(then)...%(else)...%(end).
462This prefixes the current branch with a star.
463
464------------
465git for-each-ref --format="%(if)%(HEAD)%(then)* %(else) %(end)%(refname:short)" refs/heads/
466------------
467
468
469An example to show the usage of %(if)...%(then)...%(end).
470This prints the authorname, if present.
471
472------------
473git for-each-ref --format="%(refname)%(if)%(authorname)%(then) Authored by: %(authorname)%(end)"
474------------
475
59012fe5
OT
476CAVEATS
477-------
478
479Note that the sizes of objects on disk are reported accurately, but care
480should be taken in drawing conclusions about which refs or objects are
481responsible for disk usage. The size of a packed non-delta object may be
482much larger than the size of objects which delta against it, but the
483choice of which object is the base and which is the delta is arbitrary
484and is subject to change during a repack.
485
486Note also that multiple copies of an object may be present in the object
487database; in this case, it is undefined which copy's size or delta base
488will be reported.
489
415af72b
AL
490NOTES
491-----
492
b59cdffd 493include::ref-reachability-filters.txt[]
415af72b 494
f21e1c5d
MH
495SEE ALSO
496--------
497linkgit:git-show-ref[1]
498
621c39de
AS
499GIT
500---
501Part of the linkgit:git[1] suite