]> git.ipfire.org Git - thirdparty/git.git/blame - Documentation/git-for-each-ref.txt
for-each-ref: let upstream/push optionally report the remote name
[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]
0adda936 12 [(--sort=<key>)...] [--format=<format>] [<pattern>...]
3233d51d
KD
13 [--points-at=<object>]
14 (--merged[=<object>] | --no-merged[=<object>])
15 [--contains[=<object>]] [--no-contains[=<object>]]
9f613ddd
JH
16
17DESCRIPTION
18-----------
19
20Iterate over all refs that match `<pattern>` and show them
21according to the given `<format>`, after sorting them according
d4040e0a 22to the given set of `<key>`. If `<count>` is given, stop after
23bfbb81 23showing that many refs. The interpolated values in `<format>`
9f613ddd 24can optionally be quoted as string literals in the specified
1729fa98 25host language allowing their direct evaluation in that language.
9f613ddd
JH
26
27OPTIONS
28-------
f7a32dd9
KD
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>::
9f613ddd
JH
36 By default the command shows all refs that match
37 `<pattern>`. This option makes it stop after showing
38 that many refs.
39
f7a32dd9 40--sort=<key>::
9f613ddd
JH
41 A field name to sort on. Prefix `-` to sort in
42 descending order of the value. When unspecified,
c0f6dc9b
LW
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.
9f613ddd 46
f7a32dd9 47--format=<format>::
e4933cee
MG
48 A string that interpolates `%(fieldname)` from a ref being shown
49 and the object it points at. If `fieldname`
9f613ddd 50 is prefixed with an asterisk (`*`) and the ref points
794b7e16
MG
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
ba7545ad
JN
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).
9f613ddd 59
3240240f
SB
60--shell::
61--perl::
62--python::
63--tcl::
9f613ddd
JH
64 If given, strings that substitute `%(fieldname)`
65 placeholders are quoted as string literals suitable for
66 the specified host language. This is meant to produce
67 a scriptlet that can directly be `eval`ed.
68
3233d51d 69--points-at=<object>::
d325406e 70 Only list refs which points at the given object.
9f613ddd 71
3233d51d 72--merged[=<object>]::
7c328348 73 Only list refs whose tips are reachable from the
17d6c744
ÆAB
74 specified commit (HEAD if not specified),
75 incompatible with `--no-merged`.
7c328348 76
3233d51d 77--no-merged[=<object>]::
7c328348 78 Only list refs whose tips are not reachable from the
17d6c744
ÆAB
79 specified commit (HEAD if not specified),
80 incompatible with `--merged`.
7c328348 81
3233d51d 82--contains[=<object>]::
8b5a3e98 83 Only list refs which contain the specified commit (HEAD if not
4a71109a
KN
84 specified).
85
3233d51d 86--no-contains[=<object>]::
ac3f5a34
ÆAB
87 Only list refs which don't contain the specified commit (HEAD
88 if not specified).
89
3bb16a8b
NTND
90--ignore-case::
91 Sorting and filtering refs are case insensitive.
92
9f613ddd
JH
93FIELD NAMES
94-----------
95
96Various values from structured fields in referenced objects can
97be used to interpolate into the resulting output, or as sort
98keys.
99
100For all objects, the following names can be used:
101
102refname::
69057cf3 103 The name of the ref (the part after $GIT_DIR/).
7d66f21a 104 For a non-ambiguous short name of the ref append `:short`.
2bb98169 105 The option core.warnAmbiguousRefs is used to select the strict
1a34728e
KN
106 abbreviation mode. If `lstrip=<N>` (`rstrip=<N>`) is appended, strips `<N>`
107 slash-separated path components from the front (back) of the refname
108 (e.g. `%(refname:lstrip=2)` turns `refs/tags/foo` into `foo` and
109 `%(refname:rstrip=2)` turns `refs/tags/foo` into `refs`).
1a0ca5e3 110 If `<N>` is a negative number, strip as many path components as
1a34728e 111 necessary from the specified end to leave `-<N>` path components
1a0ca5e3 112 (e.g. `%(refname:lstrip=-2)` turns
1a34728e
KN
113 `refs/tags/foo` into `tags/foo` and `%(refname:rstrip=-1)`
114 turns `refs/tags/foo` into `refs`). When the ref does not have
1a0ca5e3
KN
115 enough components, the result becomes an empty string if
116 stripping with positive <N>, or it becomes the full refname if
117 stripping with negative <N>. Neither is an error.
44a6b6ce
JH
118+
119`strip` can be used as a synomym to `lstrip`.
9f613ddd
JH
120
121objecttype::
122 The type of the object (`blob`, `tree`, `commit`, `tag`).
123
124objectsize::
0b444cdb 125 The size of the object (the same as 'git cat-file -s' reports).
9f613ddd
JH
126
127objectname::
128 The object name (aka SHA-1).
67687fea 129 For a non-ambiguous abbreviation of the object name append `:short`.
42d0eb05
KN
130 For an abbreviation of the object name with desired length append
131 `:short=<length>`, where the minimum length is MINIMUM_ABBREV. The
132 length may be exceeded to ensure unique object names.
9f613ddd 133
8cae19d9
JK
134upstream::
135 The name of a local ref which can be considered ``upstream''
1a34728e
KN
136 from the displayed ref. Respects `:short`, `:lstrip` and
137 `:rstrip` in the same way as `refname` above. Additionally
138 respects `:track` to show "[ahead N, behind M]" and
139 `:trackshort` to show the terse version: ">" (ahead), "<"
140 (behind), "<>" (ahead and behind), or "=" (in sync). `:track`
141 also prints "[gone]" whenever unknown upstream ref is
142 encountered. Append `:track,nobracket` to show tracking
cc72385f
JS
143 information without brackets (i.e "ahead N, behind M").
144+
145Also respects `:remotename` to state the name of the *remote* instead of
146the ref.
147+
148Has no effect if the ref does not have tracking information associated
149with it. All the options apart from `nobracket` are mutually exclusive,
150but if used together the last option is selected.
8cae19d9 151
29bc8850 152push::
3ba308cb 153 The name of a local ref which represents the `@{push}`
17938f17 154 location for the displayed ref. Respects `:short`, `:lstrip`,
cc72385f
JS
155 `:rstrip`, `:track`, `:trackshort` and `:remotename` options as
156 `upstream` does. Produces an empty string if no `@{push}` ref is
3ba308cb 157 configured.
29bc8850 158
7a48b832
RR
159HEAD::
160 '*' if HEAD matches current ref (the checked out branch), ' '
161 otherwise.
162
fddb74c9 163color::
5d3d0681
JK
164 Change output color. Followed by `:<colorname>`, where color
165 names are described under Values in the "CONFIGURATION FILE"
166 section of linkgit:git-config[1]. For example,
167 `%(color:bold red)`.
fddb74c9 168
ce592082
KN
169align::
170 Left-, middle-, or right-align the content between
395fb8f9
KN
171 %(align:...) and %(end). The "align:" is followed by
172 `width=<width>` and `position=<position>` in any order
173 separated by a comma, where the `<position>` is either left,
174 right or middle, default being left and `<width>` is the total
175 length of the content with alignment. For brevity, the
176 "width=" and/or "position=" prefixes may be omitted, and bare
177 <width> and <position> used instead. For instance,
178 `%(align:<width>,<position>)`. If the contents length is more
179 than the width then no alignment is performed. If used with
bcf9626a 180 `--quote` everything in between %(align:...) and %(end) is
395fb8f9
KN
181 quoted, but if nested then only the topmost level performs
182 quoting.
ce592082 183
c58492d4
KN
184if::
185 Used as %(if)...%(then)...%(end) or
186 %(if)...%(then)...%(else)...%(end). If there is an atom with
187 value or string literal after the %(if) then everything after
188 the %(then) is printed, else if the %(else) atom is used, then
189 everything after %(else) is printed. We ignore space when
190 evaluating the string before %(then), this is useful when we
191 use the %(HEAD) atom which prints either "*" or " " and we
192 want to apply the 'if' condition only on the 'HEAD' ref.
4f3e3b37
KN
193 Append ":equals=<string>" or ":notequals=<string>" to compare
194 the value between the %(if:...) and %(then) atoms with the
195 given string.
c58492d4 196
a7984101
KN
197symref::
198 The ref which the given symbolic ref refers to. If not a
1a34728e
KN
199 symbolic ref, nothing is printed. Respects the `:short`,
200 `:lstrip` and `:rstrip` options in the same way as `refname`
201 above.
a7984101 202
9f613ddd
JH
203In addition to the above, for commit and tag objects, the header
204field names (`tree`, `parent`, `object`, `type`, and `tag`) can
205be used to specify the value in the header field.
206
e914ef0d
EW
207For commit and tag objects, the special `creatordate` and `creator`
208fields will correspond to the appropriate date or name-email-date tuple
209from the `committer` or `tagger` fields depending on the object type.
210These are intended for working on a mix of annotated and lightweight tags.
211
9f613ddd
JH
212Fields that have name-email-date tuple as its value (`author`,
213`committer`, and `tagger`) can be suffixed with `name`, `email`,
214and `date` to extract the named component.
215
e2b23972 216The complete message in a commit and tag object is `contents`.
52ffe995
JW
217Its first line is `contents:subject`, where subject is the concatenation
218of all lines of the commit message up to the first blank line. The next
219line is 'contents:body', where body is all of the lines after the first
1bb38e5a
KN
220blank line. The optional GPG signature is `contents:signature`. The
221first `N` lines of the message is obtained using `contents:lines=N`.
b1d31c89
JK
222Additionally, the trailers as interpreted by linkgit:git-interpret-trailers[1]
223are obtained as 'contents:trailers'.
9f613ddd 224
e914ef0d
EW
225For sorting purposes, fields with numeric values sort in numeric order
226(`objectsize`, `authordate`, `committerdate`, `creatordate`, `taggerdate`).
9f613ddd
JH
227All other fields are used to sort in their byte-value order.
228
90c00408
KN
229There is also an option to sort by versions, this can be done by using
230the fieldname `version:refname` or its alias `v:refname`.
231
9f613ddd
JH
232In any case, a field name that refers to a field inapplicable to
233the object referred by the ref does not cause an error. It
234returns an empty string instead.
235
d392e712 236As a special case for the date-type fields, you may specify a format for
8f50d263 237the date by adding `:` followed by date format name (see the
1cca17df 238values the `--date` option to linkgit:git-rev-list[1] takes).
d392e712 239
c58492d4
KN
240Some atoms like %(align) and %(if) always require a matching %(end).
241We call them "opening atoms" and sometimes denote them as %($open).
242
243When a scripting language specific quoting is in effect, everything
244between a top-level opening atom and its matching %(end) is evaluated
245according to the semantics of the opening atom and only its result
246from the top-level is quoted.
247
9f613ddd
JH
248
249EXAMPLES
250--------
251
1729fa98 252An example directly producing formatted text. Show the most recent
22817b40 2533 tagged commits:
9f613ddd
JH
254
255------------
256#!/bin/sh
257
b1889c36 258git for-each-ref --count=3 --sort='-*authordate' \
9f613ddd
JH
259--format='From: %(*authorname) %(*authoremail)
260Subject: %(*subject)
261Date: %(*authordate)
262Ref: %(*refname)
263
264%(*body)
265' 'refs/tags'
266------------
267
1729fa98
AW
268
269A simple example showing the use of shell eval on the output,
22817b40 270demonstrating the use of --shell. List the prefixes of all heads:
1729fa98
AW
271------------
272#!/bin/sh
273
b1889c36 274git for-each-ref --shell --format="ref=%(refname)" refs/heads | \
1729fa98
AW
275while read entry
276do
277 eval "$entry"
278 echo `dirname $ref`
279done
280------------
281
282
283A bit more elaborate report on tags, demonstrating that the format
22817b40 284may be an entire script:
9f613ddd
JH
285------------
286#!/bin/sh
287
288fmt='
289 r=%(refname)
290 t=%(*objecttype)
291 T=${r#refs/tags/}
292
293 o=%(*objectname)
294 n=%(*authorname)
295 e=%(*authoremail)
296 s=%(*subject)
297 d=%(*authordate)
298 b=%(*body)
299
300 kind=Tag
301 if test "z$t" = z
302 then
303 # could be a lightweight tag
304 t=%(objecttype)
305 kind="Lightweight tag"
306 o=%(objectname)
307 n=%(authorname)
308 e=%(authoremail)
309 s=%(subject)
310 d=%(authordate)
311 b=%(body)
312 fi
313 echo "$kind $T points at a $t object $o"
314 if test "z$t" = zcommit
315 then
316 echo "The commit was authored by $n $e
317at $d, and titled
318
319 $s
320
321Its message reads as:
322"
323 echo "$b" | sed -e "s/^/ /"
324 echo
325 fi
326'
327
b1889c36 328eval=`git for-each-ref --shell --format="$fmt" \
9f613ddd
JH
329 --sort='*objecttype' \
330 --sort=-taggerdate \
331 refs/tags`
332eval "$eval"
333------------
621c39de 334
c58492d4
KN
335
336An example to show the usage of %(if)...%(then)...%(else)...%(end).
337This prefixes the current branch with a star.
338
339------------
340git for-each-ref --format="%(if)%(HEAD)%(then)* %(else) %(end)%(refname:short)" refs/heads/
341------------
342
343
344An example to show the usage of %(if)...%(then)...%(end).
345This prints the authorname, if present.
346
347------------
348git for-each-ref --format="%(refname)%(if)%(authorname)%(then) Authored by: %(authorname)%(end)"
349------------
350
f21e1c5d
MH
351SEE ALSO
352--------
353linkgit:git-show-ref[1]
354
621c39de
AS
355GIT
356---
357Part of the linkgit:git[1] suite