GIT-REV-PARSE(1) | Git Manual | GIT-REV-PARSE(1) |
NAME¶
git-rev-parse - Pick out and massage parameters
SYNOPSIS¶
git rev-parse [<options>] <arg>...
DESCRIPTION¶
Many Git porcelainish commands take a mixture of flags (i.e. parameters that begin with a dash -) and parameters meant for the underlying git rev-list command they use internally and flags and parameters for the other commands they use downstream of git rev-list. The primary purpose of this command is to allow calling programs to distinguish between them. There are a few other operation modes that have nothing to do with the above "help parse command line options".
Unless otherwise specified, most of the options and operation modes require you to run this command inside a git repository or a working tree that is under the control of a git repository, and will give you a fatal error otherwise.
OPTIONS¶
Operation Modes¶
Each of these options must appear first on the command line.
--parseopt
--sq-quote
Options for --parseopt¶
--keep-dashdash
--stop-at-non-option
--stuck-long
Options for Filtering¶
--revs-only
--no-revs
--flags
--no-flags
Options for Output¶
--default <arg>
--prefix <arg>
This can be used to convert arguments to a command run in a subdirectory so that they can still be used after moving to the top-level of the repository. For example:
prefix=$(git rev-parse --show-prefix) cd "$(git rev-parse --show-toplevel)" # rev-parse provides the -- needed for 'set' eval "set $(git rev-parse --sq --prefix "$prefix" -- "$@")"
--verify
If you want to make sure that the output actually names an object in your object database and/or can be used as a specific type of object you require, you can add the ^{type} peeling operator to the parameter. For example, git rev-parse "$VAR^{commit}" will make sure $VAR names an existing object that is a commit-ish (i.e. a commit, or an annotated tag that points at a commit). To make sure that $VAR names an existing object of any type, git rev-parse "$VAR^{object}" can be used.
Note that if you are verifying a name from an untrusted source, it is wise to use --end-of-options so that the name argument is not mistaken for another option.
-q, --quiet
--sq
--short[=<length>]
--not
--abbrev-ref[=(strict|loose)]
--symbolic
--symbolic-full-name
--output-object-format=(sha1|sha256|storage)
Specifying "sha1" translates if necessary and returns a sha1 oid.
Specifying "sha256" translates if necessary and returns a sha256 oid.
Specifying "storage" translates if necessary and returns an oid in encoded in the storage hash algorithm.
Options for Objects¶
--all
--branches[=<pattern>], --tags[=<pattern>], --remotes[=<pattern>]
If a pattern is given, only refs matching the given shell glob are shown. If the pattern does not contain a globbing character (?, *, or [), it is turned into a prefix match by appending /*.
--glob=<pattern>
--exclude=<glob-pattern>
The patterns given should not begin with refs/heads, refs/tags, or refs/remotes when applied to --branches, --tags, or --remotes, respectively, and they must begin with refs/ when applied to --glob or --all. If a trailing /* is intended, it must be given explicitly.
--exclude-hidden=(fetch|receive|uploadpack)
--disambiguate=<prefix>
Options for Files¶
--local-env-vars
--path-format=(absolute|relative)
This option may be specified multiple times and affects only the arguments that follow it on the command line, either to the end of the command line or the next instance of this option.
The following options are modified by --path-format:
--git-dir
If $GIT_DIR is not defined and the current directory is not detected to lie in a Git repository or work tree print a message to stderr and exit with nonzero status.
--git-common-dir
--resolve-git-dir <path>
--git-path <path>
--show-toplevel
--show-superproject-working-tree
--shared-index-path
The following options are unaffected by --path-format:
--absolute-git-dir
--is-inside-git-dir
--is-inside-work-tree
--is-bare-repository
--is-shallow-repository
--show-cdup
--show-prefix
--show-object-format[=(storage|input|output)]
--show-ref-format
Other Options¶
--since=<datestring>, --after=<datestring>
--until=<datestring>, --before=<datestring>
<arg>...
SPECIFYING REVISIONS¶
A revision parameter <rev> typically, but not necessarily, names a commit object. It uses what is called an extended SHA-1 syntax. Here are various ways to spell object names. The ones listed near the end of this list name trees and blobs contained in a commit.
Note
This document shows the "raw" syntax as seen by git. The shell and other UIs might require additional quoting to protect special characters and to avoid word splitting.
<sha1>, e.g. dae86e1950b1277e545cee180551750029cfe735, dae86e
<describeOutput>, e.g. v1.7.4.2-679-g3bee7fb
<refname>, e.g. master, heads/master, refs/heads/master
HEAD
FETCH_HEAD
ORIG_HEAD
MERGE_HEAD
REBASE_HEAD
REVERT_HEAD
CHERRY_PICK_HEAD
BISECT_HEAD
AUTO_MERGE
Note that any of the refs/* cases above may come either from the $GIT_DIR/refs directory or from the $GIT_DIR/packed-refs file. While the ref name encoding is unspecified, UTF-8 is preferred as some output processing may assume ref names in UTF-8.
@
[<refname>]@{<date>}, e.g. master@{yesterday}, HEAD@{5 minutes ago}
<refname>@{<n>}, e.g. master@{1}
@{<n>}, e.g. @{1}
@{-<n>}, e.g. @{-1}
[<branchname>]@{upstream}, e.g. master@{upstream}, @{u}
[<branchname>]@{push}, e.g. master@{push}, @{push}
Here’s an example to make it more clear:
$ git config push.default current $ git config remote.pushdefault myfork $ git switch -c mybranch origin/master $ git rev-parse --symbolic-full-name @{upstream} refs/remotes/origin/master $ git rev-parse --symbolic-full-name @{push} refs/remotes/myfork/mybranch
Note in the example that we set up a triangular workflow, where we pull from one location and push to another. In a non-triangular workflow, @{push} is the same as @{upstream}, and there is no need for it.
This suffix is also accepted when spelled in uppercase, and means the same thing no matter the case.
<rev>^[<n>], e.g. HEAD^, v1.5.1^0
<rev>~[<n>], e.g. HEAD~, master~3
<rev>^{<type>}, e.g. v0.99.8^{commit}
<rev>^{object} can be used to make sure <rev> names an object that exists, without requiring <rev> to be a tag, and without dereferencing <rev>; because a tag is already an object, it does not have to be dereferenced even once to get to an object.
<rev>^{tag} can be used to ensure that <rev> identifies an existing tag object.
<rev>^{}, e.g. v0.99.8^{}
<rev>^{/<text>}, e.g. HEAD^{/fix nasty bug}
:/<text>, e.g. :/fix nasty bug
<rev>:<path>, e.g. HEAD:README, master:./README
:[<n>:]<path>, e.g. :0:README, :README
Here is an illustration, by Jon Loeliger. Both commit nodes B and C are parents of commit node A. Parent commits are ordered left-to-right.
G H I J
\ / \ /
D E F
\ | / \
\ | / |
\|/ |
B C
\ /
\ /
A
A = = A^0 B = A^ = A^1 = A~1 C = = A^2 D = A^^ = A^1^1 = A~2 E = B^2 = A^^2 F = B^3 = A^^3 G = A^^^ = A^1^1^1 = A~3 H = D^2 = B^^2 = A^^^2 = A~2^2 I = F^ = B^3^ = A^^3^ J = F^2 = B^3^2 = A^^3^2
SPECIFYING RANGES¶
History traversing commands such as git log operate on a set of commits, not just a single commit.
For these commands, specifying a single revision, using the notation described in the previous section, means the set of commits reachable from the given commit.
Specifying several revisions means the set of commits reachable from any of the given commits.
A commit’s reachable set is the commit itself and the commits in its ancestry chain.
There are several notations to specify a set of connected commits (called a "revision range"), illustrated below.
Commit Exclusions¶
^<rev> (caret) Notation
Dotted Range Notations¶
The .. (two-dot) Range Notation
The ... (three-dot) Symmetric Difference Notation
In these two shorthand notations, you can omit one end and let it default to HEAD. For example, origin.. is a shorthand for origin..HEAD and asks "What did I do since I forked from the origin branch?" Similarly, ..origin is a shorthand for HEAD..origin and asks "What did the origin do since I forked from them?" Note that .. would mean HEAD..HEAD which is an empty range that is both reachable and unreachable from HEAD.
Commands that are specifically designed to take two distinct ranges (e.g. "git range-diff R1 R2" to compare two ranges) do exist, but they are exceptions. Unless otherwise noted, all "git" commands that operate on a set of commits work on a single revision range. In other words, writing two "two-dot range notation" next to each other, e.g.
$ git log A..B C..D
does not specify two revision ranges for most commands. Instead it will name a single connected set of commits, i.e. those that are reachable from either B or D but are reachable from neither A or C. In a linear history like this:
---A---B---o---o---C---D
because A and B are reachable from C, the revision range specified by these two dotted ranges is a single commit D.
Other <rev>^ Parent Shorthand Notations¶
Three other shorthands exist, particularly useful for merge commits, for naming a set that is formed by a commit and its parent commits.
The r1^@ notation means all parents of r1.
The r1^! notation includes commit r1 but excludes all of its parents. By itself, this notation denotes the single commit r1.
The <rev>^-[<n>] notation includes <rev> but excludes the <n>th parent (i.e. a shorthand for <rev>^<n>..<rev>), with <n> = 1 if not given. This is typically useful for merge commits where you can just pass <commit>^- to get all the commits in the branch that was merged in merge commit <commit> (including <commit> itself).
While <rev>^<n> was about specifying a single commit parent, these three notations also consider its parents. For example you can say HEAD^2^@, however you cannot say HEAD^@^2.
REVISION RANGE SUMMARY¶
<rev>
^<rev>
<rev1>..<rev2>
<rev1>...<rev2>
<rev>^@, e.g. HEAD^@
<rev>^!, e.g. HEAD^!
<rev>^-<n>, e.g. HEAD^-, HEAD^-2
Here are a handful of examples using the Loeliger illustration above, with each step in the notation’s expansion and selection carefully spelt out:
Args Expanded arguments Selected commits
D G H D
D F G H I J D F
^G D H D
^D B E I J F B
^D B C E I J F B C
C I J F C
B..C = ^B C C
B...C = B ^F C G H D E B C
B^- = B^..B
= ^B^1 B E I J F B
C^@ = C^1
= F I J F
B^@ = B^1 B^2 B^3
= D E F D G H E F I J
C^! = C ^C^@
= C ^C^1
= C ^F C
B^! = B ^B^@
= B ^B^1 ^B^2 ^B^3
= B ^D ^E ^F B
F^! D = F ^I ^J D G H D F
PARSEOPT¶
In --parseopt mode, git rev-parse helps massaging options to bring to shell scripts the same facilities C builtins have. It works as an option normalizer (e.g. splits single switches aggregate values), a bit like getopt(1) does.
It takes on the standard input the specification of the options to parse and understand, and echoes on the standard output a string suitable for sh(1) eval to replace the arguments with normalized ones. In case of error, it outputs usage on the standard error stream, and exits with code 129.
Note: Make sure you quote the result when passing it to eval. See below for an example.
Input Format¶
git rev-parse --parseopt input format is fully text based. It has two parts, separated by a line that contains only --. The lines before the separator (should be one or more) are used for the usage. The lines after the separator describe the options.
Each line of options has this format:
<opt-spec><flags>*<arg-hint>? SP+ help LF
<opt-spec>
<flags>
<arg-hint>
The remainder of the line, after stripping the spaces, is used as the help associated with the option.
Blank lines are ignored, and lines that don’t match this specification are used as option group headers (start the line with a space to create such lines on purpose).
Example¶
OPTS_SPEC="\ some-command [<options>] <args>... some-command does foo and bar! -- h,help! show the help foo some nifty option --foo bar= some cool option --bar with an argument baz=arg another cool option --baz with a named argument qux?path qux may take a path argument but has meaning by itself
An option group Header C? option C with an optional argument" eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
Usage text¶
When "$@" is -h or --help in the above example, the following usage text would be shown:
usage: some-command [<options>] <args>...
some-command does foo and bar!
-h, --help show the help
--[no-]foo some nifty option --foo
--[no-]bar ... some cool option --bar with an argument
--[no-]baz <arg> another cool option --baz with a named argument
--[no-]qux[=<path>] qux may take a path argument but has meaning by itself An option group Header
-C[...] option C with an optional argument
SQ-QUOTE¶
In --sq-quote mode, git rev-parse echoes on the standard output a single line suitable for sh(1) eval. This line is made by normalizing the arguments following --sq-quote. Nothing other than quoting the arguments is done.
If you want command input to still be interpreted as usual by git rev-parse before the output is shell quoted, see the --sq option.
Example¶
$ cat >your-git-script.sh <<\EOF #!/bin/sh args=$(git rev-parse --sq-quote "$@") # quote user-supplied arguments command="git frotz -n24 $args" # and use it inside a handcrafted
# command line eval "$command" EOF $ sh your-git-script.sh "a b'c"
EXAMPLES¶
$ git rev-parse --verify HEAD
$ git rev-parse --verify --end-of-options $REV^{commit}
This will error out if $REV is empty or not a valid revision.
$ git rev-parse --default master --verify --end-of-options $REV
but if $REV is empty, the commit object name from master will be printed.
GIT¶
Part of the git(1) suite
10/09/2024 | Git 2.47.0 |