git-root
Find Git repository's root directory
TLDR
Print the absolute path of the current Git repository
Print the current working directory relative to the root of the current Git repository
SYNOPSIS
git-root [path]
PARAMETERS
-h, --help
Display usage information and exit
-V, --version
Output version information and exit
DESCRIPTION
The git-root command is a lightweight utility designed to quickly identify and output the absolute path to the root directory of the Git repository containing the current working directory (or a specified path). It is especially useful for shell scripts, makefiles, IDE configurations, or any workflow where you need to reference the repository top-level without manual navigation.
Internally, it leverages git rev-parse --show-toplevel, providing a more memorable and concise alternative to the verbose Git subcommand. When invoked from within a Git repository (even a subdirectory), it prints the full path to the .git parent directory and exits successfully. If run outside a repository or in a bare repo without context, it errors out with a non-zero exit code and a message like "not a git repository".
This tool streamlines tasks like running repository-wide commands (e.g., cd $(git-root)), setting environment variables, or integrating with tools like tmux or vim plugins. While not part of core Git, it is commonly implemented as a shell alias, function, or standalone script in dotfiles and package managers across Linux distributions.
CAVEATS
Fails with error if not inside a valid Git repository.
Output is relative to specified path if provided; defaults to current directory.
Not a core Git command; requires installation or alias setup.
EXAMPLES
git-root
/home/user/my-repo
cd $(git-root)/src
Navigate to src from anywhere in repo
export REPO_ROOT=$(git-root)
Set repo root as environment variable
EXIT CODES
0: Success (root found)
1: Not a Git repository or invalid path
64+: Usage or version errors
HISTORY
Emerged around 2010 as popular shell aliases (alias git-root='git rev-parse --show-toplevel') in developer dotfiles. Later packaged in tools like grml, git-extras (since v4.0), and standalone scripts on GitHub. Gained traction with rise of monorepos and complex nested Git workflows.
SEE ALSO
git(1), git-rev-parse(1)


