Skip to content

Instantly share code, notes, and snippets.

@bezhermoso
Created January 8, 2022 16:29
Show Gist options
  • Save bezhermoso/1270d75c03c37d850cdaf0a7a8708fd0 to your computer and use it in GitHub Desktop.
Save bezhermoso/1270d75c03c37d850cdaf0a7a8708fd0 to your computer and use it in GitHub Desktop.
function jump-to-git-root {
local _root_dir="$(git rev-parse --show-toplevel 2>/dev/null)"
if [[ $? -gt 0 ]]; then
>&2 echo 'Not a Git repo!'
exit 1
fi
local _pwd=$(pwd)
if [[ $_pwd = $_root_dir ]]; then
# Handle submodules:
# If parent dir is also managed under Git then we are in a submodule.
# If so, cd to nearest Git parent project.
_root_dir="$(git -C $(dirname $_pwd) rev-parse --show-toplevel 2>/dev/null)"
if [[ $? -gt 0 ]]; then
echo "Already at Git repo root."
return 0
fi
fi
# Make `cd -` work.
OLDPWD=$_pwd
echo "Git repo root: $_root_dir"
cd $_root_dir
}
# Make short alias
alias gr=jump-to-git-root
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment