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