1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-08 17:48:01 +00:00
Files
til/git/determine-absolute-path-of-top-level-project-directory.md

1.6 KiB
Raw Blame History

Determine Absolute Path Of Top-Level Project Directory

The git rev-parse command is a git plumbing command for parsing different kinds of things in git into a canonical form that can be used in a deterministic way by scripts. I would typically think of using it to work with branch names, tags, and other kinds of refs.

There is a handy, sorta off-label use for it in determining the absolute path of the root directory for the current git repository. Use the --show-toplevel flag with no other arguments.

 git rev-parse --show-toplevel
/Users/lastword/dev/jbranchaud/til

Here, I am in the local copy of my TIL repo. This command gives me the absolute path of the top-level directory where that .git directory resides.

This is useful for scripts that need to orient themselves to the current project's top-level directory regardless of what directory they are being executed from. This is useful for things like a git hook script or monorepos with scripts located in a specific sub-project directory.

Also worth mentioning is the --show-superproject-working-tree flag. In my TIL repo, I have a private repository included as a submodule. Within that directory --show-toplevel will produce the absolute path to the submodule. If I instead want the absolute path of the super project (in this case TIL), then I can use this other flag.

 git rev-parse --show-toplevel
/Users/lastword/dev/jbranchaud/til/notes

 git rev-parse --show-superproject-working-tree
/Users/lastword/dev/jbranchaud/til

See man git-rev-parse for more details.