# cdgit: cd relative to git workdir root

3 messages from 2013-09-16 to 2013-09-17. Participants: Thomas Koch, Adam Spiers, martin f krafft.
Thread: https://gitlist.dev/t/34941

## Thomas Koch, 2013-09-16 19:01

Subject: cdgit: cd relative to git workdir root
Message-ID: <201309162101.14879.thomas@koch.ro>
URL: https://gitlist.dev/e/201309162101.14879.thomas%40koch.ro

```
Hi,

I frequently have the need to cd back to the root of the current git workdir 
and created to alias for this:

git alias root = rev-parse --show-toplevel

shell alias cdgit = cd $(git root)

Maybe somebody likes this.

Now it would be cool, to have an alias to cd into a specific subdir of the git 
worktree and have zsh (or bash) auto completion for this like

git clone git://git.kernel.org/pub/scm/git/git.git
cd git/gitweb/static/js/lib (just to be somewhere deep)
cdgit con<tab>/com<tab> (becomes: cdgit contrib/completion)

Somebody with zsh/bash skills who already did this?

Regards, Thomas Koch

```

## Adam Spiers, 2013-09-16 19:25

Subject: Re: cdgit: cd relative to git workdir root
Message-ID: <20130916192510.GA12433@gmail.com>
URL: https://gitlist.dev/e/20130916192510.GA12433%40gmail.com
In-Reply-To: <201309162101.14879.thomas@koch.ro>

```
On Mon, Sep 16, 2013 at 09:01:14PM +0200, Thomas Koch wrote:
> Hi,
> 
> I frequently have the need to cd back to the root of the current git workdir 
> and created to alias for this:
> 
> git alias root = rev-parse --show-toplevel
> 
> shell alias cdgit = cd $(git root)
> 
> Maybe somebody likes this.

I do, and I was about to copy it into my own config when I discovered
that I'd already done this 4 years ago and totally forgotten about it :)

> Now it would be cool, to have an alias to cd into a specific subdir of the git 
> worktree and have zsh (or bash) auto completion for this like
> 
> git clone git://git.kernel.org/pub/scm/git/git.git
> cd git/gitweb/static/js/lib (just to be somewhere deep)
> cdgit con<tab>/com<tab> (becomes: cdgit contrib/completion)
> 
> Somebody with zsh/bash skills who already did this?

I hadn't done this before today, but it's easy in zsh:

  https://github.com/aspiers/git-config/commit/89df1d78

Of course, _gitcd needs to be somewhere on your $fpath so it can be
autoloaded.

```

## martin f krafft, 2013-09-17 05:33

Subject: Re: cdgit: cd relative to git workdir root
Message-ID: <20130917053303.GF5390@fishbowl>
URL: https://gitlist.dev/e/20130917053303.GF5390%40fishbowl
In-Reply-To: <201309162101.14879.thomas-5j3myg3OO4w@public.gmane.org>

```
also sprach Thomas Koch <thomas-5j3myg3OO4w@public.gmane.org> [2013.09.16.2101 +0200]:
> shell alias cdgit = cd $(git root)

I've tried to make this happen many years ago, but I never finished
the ZLE widget that did it. The idea was to bind 'tab' to a function
that would replace an occurrence of ~g in $LBUFFER with the output
of rev-parse--show-toplevel and then delegate to normal tab
expansion.

So now I just have

  hash -d g=$(git rev-parse --show-toplevel)

in a pre-prompt function and get the same effect.

  fishbowl:~/code/salt|develop|debian% ls ~g/debi<tab>

-- 
martin | http://madduck.net/ | http://two.sentenc.es/
 
"i am not in favour of long engagements. they give people the
 opportunity of finding out each other's character before marriage,
 which i think is never advisable."
                                                        -- oscar wilde
 
spamtraps: madduck.bogus-JX/+c5DPh7vR7s880joybQ@public.gmane.org



```
