Git rev-parse: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(4 intermediate revisions by the same user not shown)
Line 11: Line 11:
<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
git rev-parse --abbrev-ref HEAD
git rev-parse --abbrev-ref HEAD
</syntaxhighlight>
See [[Getting the Current Git Branch]] for alternatives.
=Current Commit=
<syntaxhighlight lang='bash'>
git rev-parse HEAD
</syntaxhighlight>
</syntaxhighlight>


Line 19: Line 26:
</syntaxhighlight>
</syntaxhighlight>


If inside the root of the main work tree, return the relative path of $GIT_DIR:
If inside the root of the main work tree, return the relative path of [[Git_Environment_Variables#GIT_DIR|$GIT_DIR]]:
<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
.git
.git
</syntaxhighlight>
</syntaxhighlight>


If inside of a subdirectory of the main work tree, return the absolute path of $GIT_DIR:
If inside of a subdirectory of the main work tree, return the absolute path of [[Git_Environment_Variables#GIT_DIR|$GIT_DIR]]:
<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
/Users/someuser/my-repo/.git
/Users/someuser/my-repo/.git
</syntaxhighlight>
</syntaxhighlight>


If inside the root of a linked work tree, return the absolute path of the [[Git_Concepts#Linked_Tree_Private_Subdirectory|linked tree private subdirectory]] in the $GIT_DIR of the repository (main work tree):
If inside a linked work tree, either root or any of its subdirectories, return the absolute path of the [[Git_Concepts#Linked_Tree_Private_Subdirectory|linked tree private subdirectory]] in the $GIT_DIR of the repository (main work tree):
<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
/Users/someuser/my-repo/.git/worktrees/my-linked-tree
/Users/someuser/my-repo/.git/worktrees/my-linked-tree
Line 43: Line 50:


Returns true if inside a working tree, including the [[Git_Concepts#Main_Working_Tree|main working tree]].
Returns true if inside a working tree, including the [[Git_Concepts#Main_Working_Tree|main working tree]].
==--show-prefix==
When the command is invoked from a subdirectory, show the path of the current directory relative to the top-level directory. Works for both main work tree and linked trees. When in the root of the directory, produces empty string.
==--show-toplevel==
Show the absolute path of the top-level directory.
==--show-cdup==
When the command is invoked from a subdirectory, show the path of the top-level directory relative to the current directory (typically a sequence of "../", or an empty string).

Latest revision as of 07:03, 24 July 2020

Internal

Overview

Currently Checked Out Branch

Return the currently checked out branch in the current work tree:

git rev-parse --abbrev-ref HEAD

See Getting the Current Git Branch for alternatives.

Current Commit

git rev-parse HEAD

Other Options

--git-dir

git rev-parse --git-dir

If inside the root of the main work tree, return the relative path of $GIT_DIR:

.git

If inside of a subdirectory of the main work tree, return the absolute path of $GIT_DIR:

/Users/someuser/my-repo/.git

If inside a linked work tree, either root or any of its subdirectories, return the absolute path of the linked tree private subdirectory in the $GIT_DIR of the repository (main work tree):

/Users/someuser/my-repo/.git/worktrees/my-linked-tree

Sends

"fatal: not a git repository (or any of the parent directories): .git"

to stderr and return 128 return code if not in a git work area.

--is-inside-work-tree

Returns true if inside a working tree, including the main working tree.

--show-prefix

When the command is invoked from a subdirectory, show the path of the current directory relative to the top-level directory. Works for both main work tree and linked trees. When in the root of the directory, produces empty string.

--show-toplevel

Show the absolute path of the top-level directory.

--show-cdup

When the command is invoked from a subdirectory, show the path of the top-level directory relative to the current directory (typically a sequence of "../", or an empty string).