Skip to content

Instantly share code, notes, and snippets.

@ap
Created May 28, 2009 14:26
Show Gist options
  • Save ap/119340 to your computer and use it in GitHub Desktop.
Save ap/119340 to your computer and use it in GitHub Desktop.
shell func for cd'ing to top of git working copy
# these implementations both take special care to keep `cd -` working correctly
# good way
gcd() {
local TOP="`git rev-parse --show-cdup 2> /dev/null`" || return 1
[ "$TOP" ] && cd "$TOP"
}
# old, dumb, bad way
gcd() {
local OLDOLDPWD=$OLDPWD
pushd . > /dev/null
while : ; do
if [ -d .git ] ; then
OLDPWD="`dirs -l +1`"
popd -n > /dev/null
return
fi
[ "$PWD" = / ] && break
cd ..
done
popd > /dev/null
OLDPWD=$OLDOLDPWD
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment