When working inside a Git repository, the current directory may be several levels below top-level directory of the repository. This can make it difficult to determine the correct base path when running commands. A script, for example, may need to locate a configuration file, build output, or another project directory relative to the repository root regardless of the directory from which the script is executed. This tutorial shows how to get the root directory of Git repository.
Git provides the git rev-parse command with the --show-toplevel option for finding the root directory of the current repository. Instead of manually checking the directory structure or relying on a fixed path, this command asks Git for the absolute path of the repository top-level directory.
git rev-parse --show-toplevel
Output example:
/tmp/gitdemo
Leave a Comment
Cancel reply