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