Created
May 1, 2013 12:03
-
-
Save cgravier/5494924 to your computer and use it in GitHub Desktop.
add this to your shell rc file and whenever you are in a subfolder of a git repo, you go back to the git project root directory. "gitroot" called outside a directory as no effect except printing to stdout that you are not currently in a git repo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# @chgravier <[email protected]> | |
function gitroot { | |
FROM=$( pwd ) | |
LOOKFOR=".git" | |
GITPROJECT=0 | |
while [ "$(pwd)" != "/" ] | |
do | |
if [ ! -e $LOOKFOR ]; | |
then | |
PARENT=$(dirname $( pwd )) | |
chdir $PARENT | |
else | |
GITPROJECT=1 | |
unset PARENT | |
break; | |
fi | |
done | |
if [ $GITPROJECT -eq 1 ] | |
then | |
unset FROM | |
unset PARENT | |
echo "Switched to git project home." | |
else | |
echo "This is not a git project !" | |
cd $FROM | |
unset FROM | |
fi | |
} |
You get it for free in git plugin for oh-my-zsh, aliased to grt
https://github.com/robbyrussell/oh-my-zsh/blob/master/plugins/git/git.plugin.zsh#L71
💋
Ya mean I already had it given I have git zsh plugin ? rofl :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
or, simpler :
cd "$(git rev-parse --show-toplevel)"
🍺