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