git-rev-parse
Pick out and massage parameters
- Provided by: git-man (Version: 1:1.9.1-1ubuntu0.10)
- Source: git
- Report a bug
Pick out and massage parameters
git rev-parse [ --option ] <args>...
Many Git porcelainish commands take 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. This command is used to distinguish between them.
Each of these options must appear first on the command line.
--parseopt
--sq-quote
--keep-dashdash
--stop-at-non-option
--stuck-long
--revs-only
--no-revs
--flags
--no-flags
--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)" 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 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.
-q, --quiet
--sq
--not
--abbrev-ref[=(strict|loose)]
--short, --short=number
--symbolic
--symbolic-full-name
--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.
--disambiguate=<prefix>
--local-env-vars
--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.
--is-inside-git-dir
--is-inside-work-tree
--is-bare-repository
--resolve-git-dir <path>
--show-cdup
--show-prefix
--show-toplevel
--since=datestring, --after=datestring
--until=datestring, --before=datestring
<args>...
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.
<sha1>, e.g. dae86e1950b1277e545cee180551750029cfe735, dae86e
<describeOutput>, e.g. v1.7.4.2-679-g3bee7fb
<refname>, e.g. master, heads/master, refs/heads/master
HEAD names the commit on which you based the changes in the working tree. FETCH_HEAD records the branch which you fetched from a remote repository with your last git fetch invocation. ORIG_HEAD is created by commands that move your HEAD in a drastic way, to record the position of the HEAD before their operation, so that you can easily change the tip of the branch back to the state before you ran them. MERGE_HEAD records the commit(s) which you are merging into your branch when you run git merge. CHERRY_PICK_HEAD records the commit which you are cherry-picking when you run git cherry-pick.
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}
<rev>^, e.g. HEAD^, v1.5.1^0
<rev>~<n>, e.g. 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, :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 = 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
History traversing commands such as git log operate on a set of commits, not just a single commit. To these commands, specifying a single revision with the notation described in the previous section means the set of commits reachable from that commit, following the commit ancestry chain.
To exclude commits reachable from a commit, a prefix ^ notation is used. E.g. ^r1 r2 means commits reachable from r2 but exclude the ones reachable from r1.
This set operation appears so often that there is a shorthand for it. When you have two commits r1 and r2 (named according to the syntax explained in SPECIFYING REVISIONS above), you can ask for commits that are reachable from r2 excluding those that are reachable from r1 by ^r1 r2 and it can be written as r1..r2.
A similar notation r1...r2 is called symmetric difference of r1 and r2 and is defined as r1 r2 --not $(git merge-base --all r1 r2). It is the set of commits that are reachable from either one of r1 or r2 but not from both.
In these two shorthands, 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.
Two other shorthands for naming a set that is formed by a commit and its parent commits exist. The r1^@ notation means all parents of r1. r1^! includes commit r1 but excludes all of its parents.
To summarize:
<rev>
^<rev>
<rev1>..<rev2>
<rev1>...<rev2>
<rev>^@, e.g. HEAD^@
<rev>^!, e.g. HEAD^!
Here are a handful of examples:
D G H D D F G H I J D F ^G D H D ^D B E I J F B B..C C B...C G H D E B C ^D B C E I J F B C C I J F C C^@ I J F C^! C F^! D G H D F
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.
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 more than one) are used for the usage. The lines after the separator describe the options.
Each line of options has this format:
<opt_spec><flags>* SP+ help LF
<opt_spec>
<flags>
The remainder of the line, after stripping the spaces, is used as the help associated to 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).
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 An option group Header C? option C with an optional argument" eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
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.
$ 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"
$ git rev-parse --verify HEAD
$ git rev-parse --verify $REV^{commit}
This will error out if $REV is empty or not a valid revision.
$ git rev-parse --default master --verify $REV
but if $REV is empty, the commit object name from master will be printed.
Part of the git(1) suite