Skip to content

Instantly share code, notes, and snippets.

@qpwo
Created September 7, 2023 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qpwo/7e07f2fa881c02b77461d7c3d361ce35 to your computer and use it in GitHub Desktop.
Save qpwo/7e07f2fa881c02b77461d7c3d361ce35 to your computer and use it in GitHub Desktop.
zsh/bash alias to cd into git repo root
# append to .zshrc or .bashrc
# cd into git root
function gr() {
# check in git repo:
if ! git rev-parse --is-inside-work-tree &>/dev/null; then
echo "not in git repo"
return 1
fi
rel=$(realpath --relative-to=$(pwd) $(git rev-parse --show-toplevel))
if [[ $rel == "." ]]; then
echo "already in root"
return 0
fi
echo "cd $rel # $(git rev-parse --show-toplevel)"
cd $(git rev-parse --show-toplevel)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment