Skip to content

Instantly share code, notes, and snippets.

@cgravier
Created May 1, 2013 12:03
Show Gist options
  • Save cgravier/5494924 to your computer and use it in GitHub Desktop.
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.
# @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
}
@mehlah
Copy link

mehlah commented May 1, 2013

or, simpler :
cd "$(git rev-parse --show-toplevel)"

🍺

@mehlah
Copy link

mehlah commented May 1, 2013

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
💋

@cgravier
Copy link
Author

cgravier commented May 1, 2013

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