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