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