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 [ --option ] <args>...
10
12 Many git porcelainish commands take 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 --parseopt
20 Use git rev-parse in option parsing mode (see PARSEOPT section
21 below).
22
23 --keep-dashdash
24 Only meaningful in --parseopt mode. Tells the option parser to echo
25 out the first -- met instead of skipping it.
26
27 --stop-at-non-option
28 Only meaningful in --parseopt mode. Lets the option parser stop at
29 the first non-option argument. This can be used to parse
30 sub-commands that take options themselves.
31
32 --sq-quote
33 Use git rev-parse in shell quoting mode (see SQ-QUOTE section
34 below). In contrast to the --sq option below, this mode does only
35 quoting. Nothing else is done to command input.
36
37 --revs-only
38 Do not output flags and parameters not meant for git rev-list
39 command.
40
41 --no-revs
42 Do not output flags and parameters meant for git rev-list command.
43
44 --flags
45 Do not output non-flag parameters.
46
47 --no-flags
48 Do not output flag parameters.
49
50 --default <arg>
51 If there is no parameter given by the user, use <arg> instead.
52
53 --verify
54 The parameter given must be usable as a single, valid object name.
55 Otherwise barf and abort.
56
57 -q, --quiet
58 Only meaningful in --verify mode. Do not output an error message if
59 the first argument is not a valid object name; instead exit with
60 non-zero status silently.
61
62 --sq
63 Usually the output is made one line per flag and parameter. This
64 option makes output a single line, properly quoted for consumption
65 by shell. Useful when you expect your parameter to contain
66 whitespaces and newlines (e.g. when using pickaxe -S with git
67 diff-\*). In contrast to the --sq-quote option, the command input
68 is still interpreted as usual.
69
70 --not
71 When showing object names, prefix them with ^ and strip ^ prefix
72 from the object names that already have one.
73
74 --symbolic
75 Usually the object names are output in SHA1 form (with possible ^
76 prefix); this option makes them output in a form as close to the
77 original input as possible.
78
79 --symbolic-full-name
80 This is similar to --symbolic, but it omits input that are not refs
81 (i.e. branch or tag names; or more explicitly disambiguating
82 "heads/master" form, when you want to name the "master" branch when
83 there is an unfortunately named tag "master"), and show them as
84 full refnames (e.g. "refs/heads/master").
85
86 --abbrev-ref[={strict|loose}]
87 A non-ambiguous short name of the objects name. The option
88 core.warnAmbiguousRefs is used to select the strict abbreviation
89 mode.
90
91 --all
92 Show all refs found in refs/.
93
94 --branches[=pattern], --tags[=pattern], --remotes[=pattern]
95 Show all branches, tags, or remote-tracking branches, respectively
96 (i.e., refs found in refs/heads, refs/tags, or refs/remotes,
97 respectively).
98
99 If a pattern is given, only refs matching the given shell glob are
100 shown. If the pattern does not contain a globbing character (?, *,
101 or [), it is turned into a prefix match by appending /\*.
102
103 --glob=pattern
104 Show all refs matching the shell glob pattern pattern. If the
105 pattern does not start with refs/, this is automatically prepended.
106 If the pattern does not contain a globbing character (?, *, or [),
107 it is turned into a prefix match by appending /\*.
108
109 --show-toplevel
110 Show the absolute path of the top-level directory.
111
112 --show-prefix
113 When the command is invoked from a subdirectory, show the path of
114 the current directory relative to the top-level directory.
115
116 --show-cdup
117 When the command is invoked from a subdirectory, show the path of
118 the top-level directory relative to the current directory
119 (typically a sequence of "../", or an empty string).
120
121 --git-dir
122 Show $GIT_DIR if defined else show the path to the .git directory.
123
124 --is-inside-git-dir
125 When the current working directory is below the repository
126 directory print "true", otherwise "false".
127
128 --is-inside-work-tree
129 When the current working directory is inside the work tree of the
130 repository print "true", otherwise "false".
131
132 --is-bare-repository
133 When the repository is bare print "true", otherwise "false".
134
135 --local-env-vars
136 List the GIT_* environment variables that are local to the
137 repository (e.g. GIT_DIR or GIT_WORK_TREE, but not GIT_EDITOR).
138 Only the names of the variables are listed, not their value, even
139 if they are set.
140
141 --short, --short=number
142 Instead of outputting the full SHA1 values of object names try to
143 abbreviate them to a shorter unique name. When no length is
144 specified 7 is used. The minimum length is 4.
145
146 --since=datestring, --after=datestring
147 Parse the date string, and output the corresponding --max-age=
148 parameter for git rev-list.
149
150 --until=datestring, --before=datestring
151 Parse the date string, and output the corresponding --min-age=
152 parameter for git rev-list.
153
154 <args>...
155 Flags and parameters to be parsed.
156
158 A revision parameter typically, but not necessarily, names a commit
159 object. They use what is called an extended SHA1 syntax. Here are
160 various ways to spell object names. The ones listed near the end of
161 this list are to name trees and blobs contained in a commit.
162
163 · The full SHA1 object name (40-byte hexadecimal string), or a
164 substring of such that is unique within the repository. E.g.
165 dae86e1950b1277e545cee180551750029cfe735 and dae86e both name the
166 same commit object if there are no other object in your repository
167 whose object name starts with dae86e.
168
169 · An output from git describe; i.e. a closest tag, optionally
170 followed by a dash and a number of commits, followed by a dash, a
171 g, and an abbreviated object name.
172
173 · A symbolic ref name. E.g. master typically means the commit object
174 referenced by refs/heads/master. If you happen to have both
175 heads/master and tags/master, you can explicitly say heads/master
176 to tell git which one you mean. When ambiguous, a <name> is
177 disambiguated by taking the first match in the following rules:
178
179 1. if $GIT_DIR/<name> exists, that is what you mean (this is
180 usually useful only for HEAD, FETCH_HEAD, ORIG_HEAD and
181 MERGE_HEAD);
182
183 2. otherwise, refs/<name> if exists;
184
185 3. otherwise, refs/tags/<name> if exists;
186
187 4. otherwise, refs/heads/<name> if exists;
188
189 5. otherwise, refs/remotes/<name> if exists;
190
191 6. otherwise, refs/remotes/<name>/HEAD if exists.
192
193 HEAD names the commit your changes in the working tree is based
194 on. FETCH_HEAD records the branch you fetched from a remote
195 repository with your last git fetch invocation. ORIG_HEAD is
196 created by commands that moves your HEAD in a drastic way, to
197 record the position of the HEAD before their operation, so that
198 you can change the tip of the branch back to the state before
199 you ran them easily. MERGE_HEAD records the commit(s) you are
200 merging into your branch when you run git merge.
201
202 Note that any of the refs/* cases above may come either from
203 the $GIT_DIR/refs directory or from the $GIT_DIR/packed-refs
204 file.
205
206 · A ref followed by the suffix @ with a date specification enclosed
207 in a brace pair (e.g. {yesterday}, {1 month 2 weeks 3 days 1 hour
208 1 second ago} or {1979-02-26 18:30:00}) to specify the value of the
209 ref at a prior point in time. This suffix may only be used
210 immediately following a ref name and the ref must have an existing
211 log ($GIT_DIR/logs/<ref>). Note that this looks up the state of
212 your local ref at a given time; e.g., what was in your local master
213 branch last week. If you want to look at commits made during
214 certain times, see --since and --until.
215
216 · A ref followed by the suffix @ with an ordinal specification
217 enclosed in a brace pair (e.g. {1}, {15}) to specify the n-th
218 prior value of that ref. For example master@{1} is the immediate
219 prior value of master while master@{5} is the 5th prior value of
220 master. This suffix may only be used immediately following a ref
221 name and the ref must have an existing log ($GIT_DIR/logs/<ref>).
222
223 · You can use the @ construct with an empty ref part to get at a
224 reflog of the current branch. For example, if you are on the branch
225 blabla, then @{1} means the same as blabla@{1}.
226
227 · The special construct @{-<n>} means the <n>th branch checked out
228 before the current one.
229
230 · The suffix @{upstream} to a ref (short form ref@{u}) refers to the
231 branch the ref is set to build on top of. Missing ref defaults to
232 the current branch.
233
234 · A suffix ^ to a revision parameter means the first parent of that
235 commit object. ^<n> means the <n>th parent (i.e. rev^ is
236 equivalent to rev^1). As a special rule, rev^0 means the commit
237 itself and is used when rev is the object name of a tag object that
238 refers to a commit object.
239
240 · A suffix ~<n> to a revision parameter means the commit object that
241 is the <n>th generation grand-parent of the named commit object,
242 following only the first parent. I.e. rev~3 is equivalent to rev^^^
243 which is equivalent to rev^1^1^1. See below for a illustration of
244 the usage of this form.
245
246 · A suffix ^ followed by an object type name enclosed in brace pair
247 (e.g. v0.99.8^{commit}) means the object could be a tag, and
248 dereference the tag recursively until an object of that type is
249 found or the object cannot be dereferenced anymore (in which case,
250 barf). rev^0 introduced earlier is a short-hand for rev^{commit}.
251
252 · A suffix ^ followed by an empty brace pair (e.g. v0.99.8^{}) means
253 the object could be a tag, and dereference the tag recursively
254 until a non-tag object is found.
255
256 · A colon, followed by a slash, followed by a text: this names a
257 commit whose commit message starts with the specified text. This
258 name returns the youngest matching commit which is reachable from
259 any ref. If the commit message starts with a !, you have to repeat
260 that; the special sequence :/!, followed by something else than !
261 is reserved for now.
262
263 · A suffix : followed by a path; this names the blob or tree at the
264 given path in the tree-ish object named by the part before the
265 colon.
266
267 · A colon, optionally followed by a stage number (0 to 3) and a
268 colon, followed by a path; this names a blob object in the index at
269 the given path. Missing stage number (and the colon that follows
270 it) names a stage 0 entry. During a merge, stage 1 is the common
271 ancestor, stage 2 is the target branch’s version (typically the
272 current branch), and stage 3 is the version from the branch being
273 merged.
274
275 Here is an illustration, by Jon Loeliger. Both commit nodes B and C are
276 parents of commit node A. Parent commits are ordered left-to-right.
277
278 G H I J
279 \ / \ /
280 D E F
281 \ | / \
282 \ | / |
283 \|/ |
284 B C
285 \ /
286 \ /
287 A
288
289 A = = A^0
290 B = A^ = A^1 = A~1
291 C = A^2 = A^2
292 D = A^^ = A^1^1 = A~2
293 E = B^2 = A^^2
294 F = B^3 = A^^3
295 G = A^^^ = A^1^1^1 = A~3
296 H = D^2 = B^^2 = A^^^2 = A~2^2
297 I = F^ = B^3^ = A^^3^
298 J = F^2 = B^3^2 = A^^3^2
299
301 History traversing commands such as git log operate on a set of
302 commits, not just a single commit. To these commands, specifying a
303 single revision with the notation described in the previous section
304 means the set of commits reachable from that commit, following the
305 commit ancestry chain.
306
307 To exclude commits reachable from a commit, a prefix ^ notation is
308 used. E.g. ^r1 r2 means commits reachable from r2 but exclude the ones
309 reachable from r1.
310
311 This set operation appears so often that there is a shorthand for it.
312 When you have two commits r1 and r2 (named according to the syntax
313 explained in SPECIFYING REVISIONS above), you can ask for commits that
314 are reachable from r2 excluding those that are reachable from r1 by ^r1
315 r2 and it can be written as r1..r2.
316
317 A similar notation r1...r2 is called symmetric difference of r1 and r2
318 and is defined as r1 r2 --not $(git merge-base --all r1 r2). It is the
319 set of commits that are reachable from either one of r1 or r2 but not
320 from both.
321
322 Two other shorthands for naming a set that is formed by a commit and
323 its parent commits exist. The r1^@ notation means all parents of r1.
324 r1^! includes commit r1 but excludes all of its parents.
325
326 Here are a handful of examples:
327
328 D G H D
329 D F G H I J D F
330 ^G D H D
331 ^D B E I J F B
332 B...C G H D E B C
333 ^D B C E I J F B C
334 C^@ I J F
335 F^! D G H D F
336
338 In --parseopt mode, git rev-parse helps massaging options to bring to
339 shell scripts the same facilities C builtins have. It works as an
340 option normalizer (e.g. splits single switches aggregate values), a bit
341 like getopt(1) does.
342
343 It takes on the standard input the specification of the options to
344 parse and understand, and echoes on the standard output a line suitable
345 for sh(1) eval to replace the arguments with normalized ones. In case
346 of error, it outputs usage on the standard error stream, and exits with
347 code 129.
348
349 Input Format
350 git rev-parse --parseopt input format is fully text based. It has two
351 parts, separated by a line that contains only --. The lines before the
352 separator (should be more than one) are used for the usage. The lines
353 after the separator describe the options.
354
355 Each line of options has this format:
356
357 <opt_spec><flags>* SP+ help LF
358
359
360 <opt_spec>
361 its format is the short option character, then the long option name
362 separated by a comma. Both parts are not required, though at least
363 one is necessary. h,help, dry-run and f are all three correct
364 <opt_spec>.
365
366 <flags>
367
368 <flags> are of *, =, ? or !.
369
370 · Use = if the option takes an argument.
371
372 · Use ? to mean that the option is optional (though its use is
373 discouraged).
374
375 · Use * to mean that this option should not be listed in the
376 usage generated for the -h argument. It’s shown for --help-all
377 as documented in gitcli(7).
378
379 · Use ! to not make the corresponding negated long option
380 available.
381
382 The remainder of the line, after stripping the spaces, is used as the
383 help associated to the option.
384
385 Blank lines are ignored, and lines that don’t match this specification
386 are used as option group headers (start the line with a space to create
387 such lines on purpose).
388
389 Example
390 OPTS_SPEC="\
391 some-command [options] <args>...
392
393 some-command does foo and bar!
394 --
395 h,help show the help
396
397 foo some nifty option --foo
398 bar= some cool option --bar with an argument
399
400 An option group Header
401 C? option C with an optional argument"
402
403 eval `echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?`
404
405
407 In --sq-quote mode, git rev-parse echoes on the standard output a
408 single line suitable for sh(1) eval. This line is made by normalizing
409 the arguments following --sq-quote. Nothing other than quoting the
410 arguments is done.
411
412 If you want command input to still be interpreted as usual by git
413 rev-parse before the output is shell quoted, see the --sq option.
414
415 Example
416 $ cat >your-git-script.sh <<\EOF
417 #!/bin/sh
418 args=$(git rev-parse --sq-quote "$@") # quote user-supplied arguments
419 command="git frotz -n24 $args" # and use it inside a handcrafted
420 # command line
421 eval "$command"
422 EOF
423
424 $ sh your-git-script.sh "a b´c"
425
426
428 · Print the object name of the current commit:
429
430 $ git rev-parse --verify HEAD
431
432
433 · Print the commit object name from the revision in the $REV shell
434 variable:
435
436 $ git rev-parse --verify $REV
437
438 This will error out if $REV is empty or not a valid revision.
439
440 · Same as above:
441
442 $ git rev-parse --default master --verify $REV
443
444 but if $REV is empty, the commit object name from master will be
445 printed.
446
448 Written by Linus Torvalds <torvalds@osdl.org[1]> . Junio C Hamano
449 <gitster@pobox.com[2]> and Pierre Habouzit <madcoder@debian.org[3]>
450
452 Documentation by Junio C Hamano and the git-list
453 <git@vger.kernel.org[4]>.
454
456 Part of the git(1) suite
457
459 1. torvalds@osdl.org
460 mailto:torvalds@osdl.org
461
462 2. gitster@pobox.com
463 mailto:gitster@pobox.com
464
465 3. madcoder@debian.org
466 mailto:madcoder@debian.org
467
468 4. git@vger.kernel.org
469 mailto:git@vger.kernel.org
470
471
472
473Git 1.7.1 08/16/2017 GIT-REV-PARSE(1)