1GIT-REV-PARSE(1) Git Manual GIT-REV-PARSE(1)
2
3
4
6 git-rev-parse - Pick out and massage parameters
7
9 git rev-parse [<options>] <args>...
10
12 Many Git porcelainish commands take a mixture of flags (i.e. parameters
13 that begin with a dash -) and parameters meant for the underlying git
14 rev-list command they use internally and flags and parameters for the
15 other commands they use downstream of git rev-list. This command is
16 used to distinguish between them.
17
19 Operation Modes
20 Each of these options must appear first on the command line.
21
22 --parseopt
23 Use git rev-parse in option parsing mode (see PARSEOPT section
24 below).
25
26 --sq-quote
27 Use git rev-parse in shell quoting mode (see SQ-QUOTE section
28 below). In contrast to the --sq option below, this mode only does
29 quoting. Nothing else is done to command input.
30
31 Options for --parseopt
32 --keep-dashdash
33 Only meaningful in --parseopt mode. Tells the option parser to echo
34 out the first -- met instead of skipping it.
35
36 --stop-at-non-option
37 Only meaningful in --parseopt mode. Lets the option parser stop at
38 the first non-option argument. This can be used to parse
39 sub-commands that take options themselves.
40
41 --stuck-long
42 Only meaningful in --parseopt mode. Output the options in their
43 long form if available, and with their arguments stuck.
44
45 Options for Filtering
46 --revs-only
47 Do not output flags and parameters not meant for git rev-list
48 command.
49
50 --no-revs
51 Do not output flags and parameters meant for git rev-list command.
52
53 --flags
54 Do not output non-flag parameters.
55
56 --no-flags
57 Do not output flag parameters.
58
59 Options for Output
60 --default <arg>
61 If there is no parameter given by the user, use <arg> instead.
62
63 --prefix <arg>
64 Behave as if git rev-parse was invoked from the <arg> subdirectory
65 of the working tree. Any relative filenames are resolved as if they
66 are prefixed by <arg> and will be printed in that form.
67
68 This can be used to convert arguments to a command run in a
69 subdirectory so that they can still be used after moving to the
70 top-level of the repository. For example:
71
72 prefix=$(git rev-parse --show-prefix)
73 cd "$(git rev-parse --show-toplevel)"
74 # rev-parse provides the -- needed for 'set'
75 eval "set $(git rev-parse --sq --prefix "$prefix" -- "$@")"
76
77 --verify
78 Verify that exactly one parameter is provided, and that it can be
79 turned into a raw 20-byte SHA-1 that can be used to access the
80 object database. If so, emit it to the standard output; otherwise,
81 error out.
82
83 If you want to make sure that the output actually names an object
84 in your object database and/or can be used as a specific type of
85 object you require, you can add the ^{type} peeling operator to the
86 parameter. For example, git rev-parse "$VAR^{commit}" will make
87 sure $VAR names an existing object that is a commit-ish (i.e. a
88 commit, or an annotated tag that points at a commit). To make sure
89 that $VAR names an existing object of any type, git rev-parse
90 "$VAR^{object}" can be used.
91
92 Note that if you are verifying a name from an untrusted source, it
93 is wise to use --end-of-options so that the name argument is not
94 mistaken for another option.
95
96 -q, --quiet
97 Only meaningful in --verify mode. Do not output an error message if
98 the first argument is not a valid object name; instead exit with
99 non-zero status silently. SHA-1s for valid object names are printed
100 to stdout on success.
101
102 --sq
103 Usually the output is made one line per flag and parameter. This
104 option makes output a single line, properly quoted for consumption
105 by shell. Useful when you expect your parameter to contain
106 whitespaces and newlines (e.g. when using pickaxe -S with git
107 diff-*). In contrast to the --sq-quote option, the command input is
108 still interpreted as usual.
109
110 --short[=length]
111 Same as --verify but shortens the object name to a unique prefix
112 with at least length characters. The minimum length is 4, the
113 default is the effective value of the core.abbrev configuration
114 variable (see git-config(1)).
115
116 --not
117 When showing object names, prefix them with ^ and strip ^ prefix
118 from the object names that already have one.
119
120 --abbrev-ref[=(strict|loose)]
121 A non-ambiguous short name of the objects name. The option
122 core.warnAmbiguousRefs is used to select the strict abbreviation
123 mode.
124
125 --symbolic
126 Usually the object names are output in SHA-1 form (with possible ^
127 prefix); this option makes them output in a form as close to the
128 original input as possible.
129
130 --symbolic-full-name
131 This is similar to --symbolic, but it omits input that are not refs
132 (i.e. branch or tag names; or more explicitly disambiguating
133 "heads/master" form, when you want to name the "master" branch when
134 there is an unfortunately named tag "master"), and shows them as
135 full refnames (e.g. "refs/heads/master").
136
137 Options for Objects
138 --all
139 Show all refs found in refs/.
140
141 --branches[=pattern], --tags[=pattern], --remotes[=pattern]
142 Show all branches, tags, or remote-tracking branches, respectively
143 (i.e., refs found in refs/heads, refs/tags, or refs/remotes,
144 respectively).
145
146 If a pattern is given, only refs matching the given shell glob are
147 shown. If the pattern does not contain a globbing character (?, *,
148 or [), it is turned into a prefix match by appending /*.
149
150 --glob=pattern
151 Show all refs matching the shell glob pattern pattern. If the
152 pattern does not start with refs/, this is automatically prepended.
153 If the pattern does not contain a globbing character (?, *, or [),
154 it is turned into a prefix match by appending /*.
155
156 --exclude=<glob-pattern>
157 Do not include refs matching <glob-pattern> that the next --all,
158 --branches, --tags, --remotes, or --glob would otherwise consider.
159 Repetitions of this option accumulate exclusion patterns up to the
160 next --all, --branches, --tags, --remotes, or --glob option (other
161 options or arguments do not clear accumulated patterns).
162
163 The patterns given should not begin with refs/heads, refs/tags, or
164 refs/remotes when applied to --branches, --tags, or --remotes,
165 respectively, and they must begin with refs/ when applied to --glob
166 or --all. If a trailing /* is intended, it must be given
167 explicitly.
168
169 --exclude-hidden=[fetch|receive|uploadpack]
170 Do not include refs that would be hidden by git-fetch,
171 git-receive-pack or git-upload-pack by consulting the appropriate
172 fetch.hideRefs, receive.hideRefs or uploadpack.hideRefs
173 configuration along with transfer.hideRefs (see git-config(1)).
174 This option affects the next pseudo-ref option --all or --glob and
175 is cleared after processing them.
176
177 --disambiguate=<prefix>
178 Show every object whose name begins with the given prefix. The
179 <prefix> must be at least 4 hexadecimal digits long to avoid
180 listing each and every object in the repository by mistake.
181
182 Options for Files
183 --local-env-vars
184 List the GIT_* environment variables that are local to the
185 repository (e.g. GIT_DIR or GIT_WORK_TREE, but not GIT_EDITOR).
186 Only the names of the variables are listed, not their value, even
187 if they are set.
188
189 --path-format=(absolute|relative)
190 Controls the behavior of certain other options. If specified as
191 absolute, the paths printed by those options will be absolute and
192 canonical. If specified as relative, the paths will be relative to
193 the current working directory if that is possible. The default is
194 option specific.
195
196 This option may be specified multiple times and affects only the
197 arguments that follow it on the command line, either to the end of
198 the command line or the next instance of this option.
199
200 The following options are modified by --path-format:
201
202 --git-dir
203 Show $GIT_DIR if defined. Otherwise show the path to the .git
204 directory. The path shown, when relative, is relative to the
205 current working directory.
206
207 If $GIT_DIR is not defined and the current directory is not
208 detected to lie in a Git repository or work tree print a message to
209 stderr and exit with nonzero status.
210
211 --git-common-dir
212 Show $GIT_COMMON_DIR if defined, else $GIT_DIR.
213
214 --resolve-git-dir <path>
215 Check if <path> is a valid repository or a gitfile that points at a
216 valid repository, and print the location of the repository. If
217 <path> is a gitfile then the resolved path to the real repository
218 is printed.
219
220 --git-path <path>
221 Resolve "$GIT_DIR/<path>" and takes other path relocation variables
222 such as $GIT_OBJECT_DIRECTORY, $GIT_INDEX_FILE... into account. For
223 example, if $GIT_OBJECT_DIRECTORY is set to /foo/bar then "git
224 rev-parse --git-path objects/abc" returns /foo/bar/abc.
225
226 --show-toplevel
227 Show the (by default, absolute) path of the top-level directory of
228 the working tree. If there is no working tree, report an error.
229
230 --show-superproject-working-tree
231 Show the absolute path of the root of the superproject’s working
232 tree (if exists) that uses the current repository as its submodule.
233 Outputs nothing if the current repository is not used as a
234 submodule by any project.
235
236 --shared-index-path
237 Show the path to the shared index file in split index mode, or
238 empty if not in split-index mode.
239
240 The following options are unaffected by --path-format:
241
242 --absolute-git-dir
243 Like --git-dir, but its output is always the canonicalized absolute
244 path.
245
246 --is-inside-git-dir
247 When the current working directory is below the repository
248 directory print "true", otherwise "false".
249
250 --is-inside-work-tree
251 When the current working directory is inside the work tree of the
252 repository print "true", otherwise "false".
253
254 --is-bare-repository
255 When the repository is bare print "true", otherwise "false".
256
257 --is-shallow-repository
258 When the repository is shallow print "true", otherwise "false".
259
260 --show-cdup
261 When the command is invoked from a subdirectory, show the path of
262 the top-level directory relative to the current directory
263 (typically a sequence of "../", or an empty string).
264
265 --show-prefix
266 When the command is invoked from a subdirectory, show the path of
267 the current directory relative to the top-level directory.
268
269 --show-object-format[=(storage|input|output)]
270 Show the object format (hash algorithm) used for the repository for
271 storage inside the .git directory, input, or output. For input,
272 multiple algorithms may be printed, space-separated. If not
273 specified, the default is "storage".
274
275 Other Options
276 --since=datestring, --after=datestring
277 Parse the date string, and output the corresponding --max-age=
278 parameter for git rev-list.
279
280 --until=datestring, --before=datestring
281 Parse the date string, and output the corresponding --min-age=
282 parameter for git rev-list.
283
284 <args>...
285 Flags and parameters to be parsed.
286
288 A revision parameter <rev> typically, but not necessarily, names a
289 commit object. It uses what is called an extended SHA-1 syntax. Here
290 are various ways to spell object names. The ones listed near the end of
291 this list name trees and blobs contained in a commit.
292
293 Note
294 This document shows the "raw" syntax as seen by git. The shell and
295 other UIs might require additional quoting to protect special
296 characters and to avoid word splitting.
297
298 <sha1>, e.g. dae86e1950b1277e545cee180551750029cfe735, dae86e
299 The full SHA-1 object name (40-byte hexadecimal string), or a
300 leading substring that is unique within the repository. E.g.
301 dae86e1950b1277e545cee180551750029cfe735 and dae86e both name the
302 same commit object if there is no other object in your repository
303 whose object name starts with dae86e.
304
305 <describeOutput>, e.g. v1.7.4.2-679-g3bee7fb
306 Output from git describe; i.e. a closest tag, optionally followed
307 by a dash and a number of commits, followed by a dash, a g, and an
308 abbreviated object name.
309
310 <refname>, e.g. master, heads/master, refs/heads/master
311 A symbolic ref name. E.g. master typically means the commit object
312 referenced by refs/heads/master. If you happen to have both
313 heads/master and tags/master, you can explicitly say heads/master
314 to tell Git which one you mean. When ambiguous, a <refname> is
315 disambiguated by taking the first match in the following rules:
316
317 1. If $GIT_DIR/<refname> exists, that is what you mean (this is
318 usually useful only for HEAD, FETCH_HEAD, ORIG_HEAD,
319 MERGE_HEAD, REBASE_HEAD, REVERT_HEAD, CHERRY_PICK_HEAD,
320 BISECT_HEAD and AUTO_MERGE);
321
322 2. otherwise, refs/<refname> if it exists;
323
324 3. otherwise, refs/tags/<refname> if it exists;
325
326 4. otherwise, refs/heads/<refname> if it exists;
327
328 5. otherwise, refs/remotes/<refname> if it exists;
329
330 6. otherwise, refs/remotes/<refname>/HEAD if it exists.
331
332 HEAD
333 names the commit on which you based the changes in the
334 working tree.
335
336 FETCH_HEAD
337 records the branch which you fetched from a remote
338 repository with your last git fetch invocation.
339
340 ORIG_HEAD
341 is created by commands that move your HEAD in a drastic way
342 (git am, git merge, git rebase, git reset), to record the
343 position of the HEAD before their operation, so that you
344 can easily change the tip of the branch back to the state
345 before you ran them.
346
347 MERGE_HEAD
348 records the commit(s) which you are merging into your
349 branch when you run git merge.
350
351 REBASE_HEAD
352 during a rebase, records the commit at which the operation
353 is currently stopped, either because of conflicts or an
354 edit command in an interactive rebase.
355
356 REVERT_HEAD
357 records the commit which you are reverting when you run git
358 revert.
359
360 CHERRY_PICK_HEAD
361 records the commit which you are cherry-picking when you
362 run git cherry-pick.
363
364 BISECT_HEAD
365 records the current commit to be tested when you run git
366 bisect --no-checkout.
367
368 AUTO_MERGE
369 records a tree object corresponding to the state the ort
370 merge strategy wrote to the working tree when a merge
371 operation resulted in conflicts.
372
373 Note that any of the refs/* cases above may come either from the
374 $GIT_DIR/refs directory or from the $GIT_DIR/packed-refs file.
375 While the ref name encoding is unspecified, UTF-8 is preferred as
376 some output processing may assume ref names in UTF-8.
377
378 @
379 @ alone is a shortcut for HEAD.
380
381 [<refname>]@{<date>}, e.g. master@{yesterday}, HEAD@{5 minutes ago}
382 A ref followed by the suffix @ with a date specification enclosed
383 in a brace pair (e.g. {yesterday}, {1 month 2 weeks 3 days 1 hour
384 1 second ago} or {1979-02-26 18:30:00}) specifies the value of the
385 ref at a prior point in time. This suffix may only be used
386 immediately following a ref name and the ref must have an existing
387 log ($GIT_DIR/logs/<ref>). Note that this looks up the state of
388 your local ref at a given time; e.g., what was in your local master
389 branch last week. If you want to look at commits made during
390 certain times, see --since and --until.
391
392 <refname>@{<n>}, e.g. master@{1}
393 A ref followed by the suffix @ with an ordinal specification
394 enclosed in a brace pair (e.g. {1}, {15}) specifies the n-th prior
395 value of that ref. For example master@{1} is the immediate prior
396 value of master while master@{5} is the 5th prior value of master.
397 This suffix may only be used immediately following a ref name and
398 the ref must have an existing log ($GIT_DIR/logs/<refname>).
399
400 @{<n>}, e.g. @{1}
401 You can use the @ construct with an empty ref part to get at a
402 reflog entry of the current branch. For example, if you are on
403 branch blabla then @{1} means the same as blabla@{1}.
404
405 @{-<n>}, e.g. @{-1}
406 The construct @{-<n>} means the <n>th branch/commit checked out
407 before the current one.
408
409 [<branchname>]@{upstream}, e.g. master@{upstream}, @{u}
410 A branch B may be set up to build on top of a branch X (configured
411 with branch.<name>.merge) at a remote R (configured with
412 branch.<name>.remote). B@{u} refers to the remote-tracking branch
413 for the branch X taken from remote R, typically found at
414 refs/remotes/R/X.
415
416 [<branchname>]@{push}, e.g. master@{push}, @{push}
417 The suffix @{push} reports the branch "where we would push to" if
418 git push were run while branchname was checked out (or the current
419 HEAD if no branchname is specified). Like for @{upstream}, we
420 report the remote-tracking branch that corresponds to that branch
421 at the remote.
422
423 Here’s an example to make it more clear:
424
425 $ git config push.default current
426 $ git config remote.pushdefault myfork
427 $ git switch -c mybranch origin/master
428
429 $ git rev-parse --symbolic-full-name @{upstream}
430 refs/remotes/origin/master
431
432 $ git rev-parse --symbolic-full-name @{push}
433 refs/remotes/myfork/mybranch
434
435 Note in the example that we set up a triangular workflow, where we
436 pull from one location and push to another. In a non-triangular
437 workflow, @{push} is the same as @{upstream}, and there is no need
438 for it.
439
440 This suffix is also accepted when spelled in uppercase, and means
441 the same thing no matter the case.
442
443 <rev>^[<n>], e.g. HEAD^, v1.5.1^0
444 A suffix ^ to a revision parameter means the first parent of that
445 commit object. ^<n> means the <n>th parent (i.e. <rev>^ is
446 equivalent to <rev>^1). As a special rule, <rev>^0 means the commit
447 itself and is used when <rev> is the object name of a tag object
448 that refers to a commit object.
449
450 <rev>~[<n>], e.g. HEAD~, master~3
451 A suffix ~ to a revision parameter means the first parent of that
452 commit object. A suffix ~<n> to a revision parameter means the
453 commit object that is the <n>th generation ancestor of the named
454 commit object, following only the first parents. I.e. <rev>~3 is
455 equivalent to <rev>^^^ which is equivalent to <rev>^1^1^1. See
456 below for an illustration of the usage of this form.
457
458 <rev>^{<type>}, e.g. v0.99.8^{commit}
459 A suffix ^ followed by an object type name enclosed in brace pair
460 means dereference the object at <rev> recursively until an object
461 of type <type> is found or the object cannot be dereferenced
462 anymore (in which case, barf). For example, if <rev> is a
463 commit-ish, <rev>^{commit} describes the corresponding commit
464 object. Similarly, if <rev> is a tree-ish, <rev>^{tree} describes
465 the corresponding tree object. <rev>^0 is a short-hand for
466 <rev>^{commit}.
467
468 <rev>^{object} can be used to make sure <rev> names an object that
469 exists, without requiring <rev> to be a tag, and without
470 dereferencing <rev>; because a tag is already an object, it does
471 not have to be dereferenced even once to get to an object.
472
473 <rev>^{tag} can be used to ensure that <rev> identifies an existing
474 tag object.
475
476 <rev>^{}, e.g. v0.99.8^{}
477 A suffix ^ followed by an empty brace pair means the object could
478 be a tag, and dereference the tag recursively until a non-tag
479 object is found.
480
481 <rev>^{/<text>}, e.g. HEAD^{/fix nasty bug}
482 A suffix ^ to a revision parameter, followed by a brace pair that
483 contains a text led by a slash, is the same as the :/fix nasty bug
484 syntax below except that it returns the youngest matching commit
485 which is reachable from the <rev> before ^.
486
487 :/<text>, e.g. :/fix nasty bug
488 A colon, followed by a slash, followed by a text, names a commit
489 whose commit message matches the specified regular expression. This
490 name returns the youngest matching commit which is reachable from
491 any ref, including HEAD. The regular expression can match any part
492 of the commit message. To match messages starting with a string,
493 one can use e.g. :/^foo. The special sequence :/! is reserved for
494 modifiers to what is matched. :/!-foo performs a negative match,
495 while :/!!foo matches a literal ! character, followed by foo. Any
496 other sequence beginning with :/! is reserved for now. Depending
497 on the given text, the shell’s word splitting rules might require
498 additional quoting.
499
500 <rev>:<path>, e.g. HEAD:README, master:./README
501 A suffix : followed by a path names the blob or tree at the given
502 path in the tree-ish object named by the part before the colon. A
503 path starting with ./ or ../ is relative to the current working
504 directory. The given path will be converted to be relative to the
505 working tree’s root directory. This is most useful to address a
506 blob or tree from a commit or tree that has the same tree structure
507 as the working tree.
508
509 :[<n>:]<path>, e.g. :0:README, :README
510 A colon, optionally followed by a stage number (0 to 3) and a
511 colon, followed by a path, names a blob object in the index at the
512 given path. A missing stage number (and the colon that follows it)
513 names a stage 0 entry. During a merge, stage 1 is the common
514 ancestor, stage 2 is the target branch’s version (typically the
515 current branch), and stage 3 is the version from the branch which
516 is being merged.
517
518 Here is an illustration, by Jon Loeliger. Both commit nodes B and C are
519 parents of commit node A. Parent commits are ordered left-to-right.
520
521 G H I J
522 \ / \ /
523 D E F
524 \ | / \
525 \ | / |
526 \|/ |
527 B C
528 \ /
529 \ /
530 A
531
532 A = = A^0
533 B = A^ = A^1 = A~1
534 C = = A^2
535 D = A^^ = A^1^1 = A~2
536 E = B^2 = A^^2
537 F = B^3 = A^^3
538 G = A^^^ = A^1^1^1 = A~3
539 H = D^2 = B^^2 = A^^^2 = A~2^2
540 I = F^ = B^3^ = A^^3^
541 J = F^2 = B^3^2 = A^^3^2
542
544 History traversing commands such as git log operate on a set of
545 commits, not just a single commit.
546
547 For these commands, specifying a single revision, using the notation
548 described in the previous section, means the set of commits reachable
549 from the given commit.
550
551 Specifying several revisions means the set of commits reachable from
552 any of the given commits.
553
554 A commit’s reachable set is the commit itself and the commits in its
555 ancestry chain.
556
557 There are several notations to specify a set of connected commits
558 (called a "revision range"), illustrated below.
559
560 Commit Exclusions
561 ^<rev> (caret) Notation
562 To exclude commits reachable from a commit, a prefix ^ notation is
563 used. E.g. ^r1 r2 means commits reachable from r2 but exclude the
564 ones reachable from r1 (i.e. r1 and its ancestors).
565
566 Dotted Range Notations
567 The .. (two-dot) Range Notation
568 The ^r1 r2 set operation appears so often that there is a shorthand
569 for it. When you have two commits r1 and r2 (named according to the
570 syntax explained in SPECIFYING REVISIONS above), you can ask for
571 commits that are reachable from r2 excluding those that are
572 reachable from r1 by ^r1 r2 and it can be written as r1..r2.
573
574 The ... (three-dot) Symmetric Difference Notation
575 A similar notation r1...r2 is called symmetric difference of r1 and
576 r2 and is defined as r1 r2 --not $(git merge-base --all r1 r2). It
577 is the set of commits that are reachable from either one of r1
578 (left side) or r2 (right side) but not from both.
579
580 In these two shorthand notations, you can omit one end and let it
581 default to HEAD. For example, origin.. is a shorthand for origin..HEAD
582 and asks "What did I do since I forked from the origin branch?"
583 Similarly, ..origin is a shorthand for HEAD..origin and asks "What did
584 the origin do since I forked from them?" Note that .. would mean
585 HEAD..HEAD which is an empty range that is both reachable and
586 unreachable from HEAD.
587
588 Commands that are specifically designed to take two distinct ranges
589 (e.g. "git range-diff R1 R2" to compare two ranges) do exist, but they
590 are exceptions. Unless otherwise noted, all "git" commands that operate
591 on a set of commits work on a single revision range. In other words,
592 writing two "two-dot range notation" next to each other, e.g.
593
594 $ git log A..B C..D
595
596 does not specify two revision ranges for most commands. Instead it will
597 name a single connected set of commits, i.e. those that are reachable
598 from either B or D but are reachable from neither A or C. In a linear
599 history like this:
600
601 ---A---B---o---o---C---D
602
603 because A and B are reachable from C, the revision range specified by
604 these two dotted ranges is a single commit D.
605
606 Other <rev>^ Parent Shorthand Notations
607 Three other shorthands exist, particularly useful for merge commits,
608 for naming a set that is formed by a commit and its parent commits.
609
610 The r1^@ notation means all parents of r1.
611
612 The r1^! notation includes commit r1 but excludes all of its parents.
613 By itself, this notation denotes the single commit r1.
614
615 The <rev>^-[<n>] notation includes <rev> but excludes the <n>th parent
616 (i.e. a shorthand for <rev>^<n>..<rev>), with <n> = 1 if not given.
617 This is typically useful for merge commits where you can just pass
618 <commit>^- to get all the commits in the branch that was merged in
619 merge commit <commit> (including <commit> itself).
620
621 While <rev>^<n> was about specifying a single commit parent, these
622 three notations also consider its parents. For example you can say
623 HEAD^2^@, however you cannot say HEAD^@^2.
624
626 <rev>
627 Include commits that are reachable from <rev> (i.e. <rev> and its
628 ancestors).
629
630 ^<rev>
631 Exclude commits that are reachable from <rev> (i.e. <rev> and its
632 ancestors).
633
634 <rev1>..<rev2>
635 Include commits that are reachable from <rev2> but exclude those
636 that are reachable from <rev1>. When either <rev1> or <rev2> is
637 omitted, it defaults to HEAD.
638
639 <rev1>...<rev2>
640 Include commits that are reachable from either <rev1> or <rev2> but
641 exclude those that are reachable from both. When either <rev1> or
642 <rev2> is omitted, it defaults to HEAD.
643
644 <rev>^@, e.g. HEAD^@
645 A suffix ^ followed by an at sign is the same as listing all
646 parents of <rev> (meaning, include anything reachable from its
647 parents, but not the commit itself).
648
649 <rev>^!, e.g. HEAD^!
650 A suffix ^ followed by an exclamation mark is the same as giving
651 commit <rev> and all its parents prefixed with ^ to exclude them
652 (and their ancestors).
653
654 <rev>^-<n>, e.g. HEAD^-, HEAD^-2
655 Equivalent to <rev>^<n>..<rev>, with <n> = 1 if not given.
656
657 Here are a handful of examples using the Loeliger illustration above,
658 with each step in the notation’s expansion and selection carefully
659 spelt out:
660
661 Args Expanded arguments Selected commits
662 D G H D
663 D F G H I J D F
664 ^G D H D
665 ^D B E I J F B
666 ^D B C E I J F B C
667 C I J F C
668 B..C = ^B C C
669 B...C = B ^F C G H D E B C
670 B^- = B^..B
671 = ^B^1 B E I J F B
672 C^@ = C^1
673 = F I J F
674 B^@ = B^1 B^2 B^3
675 = D E F D G H E F I J
676 C^! = C ^C^@
677 = C ^C^1
678 = C ^F C
679 B^! = B ^B^@
680 = B ^B^1 ^B^2 ^B^3
681 = B ^D ^E ^F B
682 F^! D = F ^I ^J D G H D F
683
685 In --parseopt mode, git rev-parse helps massaging options to bring to
686 shell scripts the same facilities C builtins have. It works as an
687 option normalizer (e.g. splits single switches aggregate values), a bit
688 like getopt(1) does.
689
690 It takes on the standard input the specification of the options to
691 parse and understand, and echoes on the standard output a string
692 suitable for sh(1) eval to replace the arguments with normalized ones.
693 In case of error, it outputs usage on the standard error stream, and
694 exits with code 129.
695
696 Note: Make sure you quote the result when passing it to eval. See below
697 for an example.
698
699 Input Format
700 git rev-parse --parseopt input format is fully text based. It has two
701 parts, separated by a line that contains only --. The lines before the
702 separator (should be one or more) are used for the usage. The lines
703 after the separator describe the options.
704
705 Each line of options has this format:
706
707 <opt-spec><flags>*<arg-hint>? SP+ help LF
708
709 <opt-spec>
710 its format is the short option character, then the long option name
711 separated by a comma. Both parts are not required, though at least
712 one is necessary. May not contain any of the <flags> characters.
713 h,help, dry-run and f are examples of correct <opt-spec>.
714
715 <flags>
716 <flags> are of *, =, ? or !.
717
718 • Use = if the option takes an argument.
719
720 • Use ? to mean that the option takes an optional argument. You
721 probably want to use the --stuck-long mode to be able to
722 unambiguously parse the optional argument.
723
724 • Use * to mean that this option should not be listed in the
725 usage generated for the -h argument. It’s shown for --help-all
726 as documented in gitcli(7).
727
728 • Use ! to not make the corresponding negated long option
729 available.
730
731 <arg-hint>
732 <arg-hint>, if specified, is used as a name of the argument in the
733 help output, for options that take arguments. <arg-hint> is
734 terminated by the first whitespace. It is customary to use a dash
735 to separate words in a multi-word argument hint.
736
737 The remainder of the line, after stripping the spaces, is used as the
738 help associated with the option.
739
740 Blank lines are ignored, and lines that don’t match this specification
741 are used as option group headers (start the line with a space to create
742 such lines on purpose).
743
744 Example
745 OPTS_SPEC="\
746 some-command [<options>] <args>...
747
748 some-command does foo and bar!
749 --
750 h,help! show the help
751
752 foo some nifty option --foo
753 bar= some cool option --bar with an argument
754 baz=arg another cool option --baz with a named argument
755 qux?path qux may take a path argument but has meaning by itself
756
757 An option group Header
758 C? option C with an optional argument"
759
760 eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
761
762 Usage text
763 When "$@" is -h or --help in the above example, the following usage
764 text would be shown:
765
766 usage: some-command [<options>] <args>...
767
768 some-command does foo and bar!
769
770 -h, --help show the help
771 --[no-]foo some nifty option --foo
772 --[no-]bar ... some cool option --bar with an argument
773 --[no-]baz <arg> another cool option --baz with a named argument
774 --[no-]qux[=<path>] qux may take a path argument but has meaning by itself
775
776 An option group Header
777 -C[...] option C with an optional argument
778
780 In --sq-quote mode, git rev-parse echoes on the standard output a
781 single line suitable for sh(1) eval. This line is made by normalizing
782 the arguments following --sq-quote. Nothing other than quoting the
783 arguments is done.
784
785 If you want command input to still be interpreted as usual by git
786 rev-parse before the output is shell quoted, see the --sq option.
787
788 Example
789 $ cat >your-git-script.sh <<\EOF
790 #!/bin/sh
791 args=$(git rev-parse --sq-quote "$@") # quote user-supplied arguments
792 command="git frotz -n24 $args" # and use it inside a handcrafted
793 # command line
794 eval "$command"
795 EOF
796
797 $ sh your-git-script.sh "a b'c"
798
800 • Print the object name of the current commit:
801
802 $ git rev-parse --verify HEAD
803
804 • Print the commit object name from the revision in the $REV shell
805 variable:
806
807 $ git rev-parse --verify --end-of-options $REV^{commit}
808
809 This will error out if $REV is empty or not a valid revision.
810
811 • Similar to above:
812
813 $ git rev-parse --default master --verify --end-of-options $REV
814
815 but if $REV is empty, the commit object name from master will be
816 printed.
817
819 Part of the git(1) suite
820
821
822
823Git 2.43.0 11/20/2023 GIT-REV-PARSE(1)