prezto icon indicating copy to clipboard operation
prezto copied to clipboard

git: add alias that allows cd to the git-root

Open cgundogan opened this issue 9 years ago • 4 comments

This is an alias that I use quite often to change quickly to the top-level directory of a git repo. I thought it might be a good idea to push this upstream, so that others can also benefit from it.

cgundogan avatar Sep 16 '15 09:09 cgundogan

I ask to have g.. be considered over grt, as it is more intuitive.

Eriner avatar Nov 03 '15 01:11 Eriner

@Eriner I adjusted the grt alias to be g.. and amended the change immediately. You are right that it is more intuitive this way. Thanks for the feedback.

cgundogan avatar Nov 03 '15 11:11 cgundogan

No problem; sorry for not providing attribution correctly in my original PR, I didn't find it when searching. Great alias :+1:

Eriner avatar Nov 03 '15 11:11 Eriner

Old thread, but thought I would share the version I use. I went with gcd for the alias.

# Inspired by http://jeetworks.org/node/52

cdup=$(git rev-parse --show-cdup)
# git rev-parse returns non-zero when not in a git repository
if [ $? -eq 0 ]; then
    # and an empty string when you're already at the root
    target=${cdup}${1}
    if [ -n "$target" ]; then
        cd $target
    fi
fi

It supports the usage above, and also lets you gcd modules/prompt/functions.

I've got a completion for it too, which is super handy, but it seems to be broken at the moment.

paulmelnikow avatar Mar 04 '17 20:03 paulmelnikow