Skip to content

Instantly share code, notes, and snippets.

@cxw42
Created May 24, 2019 19:59
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 cxw42/2c8e86132a7d7d0d2ee6121c5cb19a38 to your computer and use it in GitHub Desktop.
Save cxw42/2c8e86132a7d7d0d2ee6121c5cb19a38 to your computer and use it in GitHub Desktop.
cd to the root of a git repo, even from .git or from submodules (bash)
# Edit as noted below, then source this from your .bashrc, or paste it in there. Then
# cdgr
# will move you to the root of the current git repo (if any).
# If you are in a submodule, it will move you to the top of the containing repo.
# Edits:
# - If you are not on Cygwin, remove line 16
# - If you are on Cygwin, change YOUR_USERNAME on line 16 to ... your username ;) .
# This is to map from /cygdrive paths back to Cygwin-native paths.
function cdgr_munge_dirs() {
sed '
s#^true$#'"${PWD%%/.git*}"'# ;
1 {
s#/cygdrive/c/Users/YOUR_USERNAME#'"$HOME"'#i ;
q
}'
}
alias cdgr='cd "$(git rev-parse --show-superproject-working-tree --show-toplevel --is-inside-git-dir | cdgr_munge_dirs)"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment