In Git it's possible to get the tree of a sub-directory, say foo/ like this
git ls-tree -d HEAD foo
but suppose foo is the current directory, how does one get the tree of that directory?
The following
git ls-tree -d HEAD .
would list any trees in foo, but not the tree of foo.
The best approach I have found is
git ls-tree -d HEAD $PWD
but I wonder if there is way to do this without relying on the $PWD environment variable.
As commented, that would be:
The
$(pwd)command substitution will execute thepwdcommand, which prints the working directory, and substitute its output into the command. That way, you are not directly relying on the$PWDenvironment variable. However, this still involves obtaining the path of the current directory.Another alternative is to use a combination of
git rev-parseandgit ls-treecommands. Here is how you could do it:The
git rev-parse --show-prefixcommand returns the prefix (i.e., the path from the repository root to the current directory), which can then be used with thegit ls-treecommand to obtain the tree of the current directory.