`gcd` - A Git-aware `cd` Relative to the Repository Root with Auto-Completion

The following will enable you to have a Git-aware “cd” command with directory path expansion/auto-completion relative to the repository root. You will have to source it into your “~/.bashrc” file, after which invoking “gcd” from the shell will allow you specify directory paths relative to the root of your Git repository no matter where you are within the working tree.

gcd()
_gcd()
"
        prev="$$2"
        dirnames=$(cd $TARGET; compgen -o dirnames $2)
        opts=$(for i in $dirnames; do  if [[ $i != ".git" ]]; then echo $i/; fi; done)
        if [[ $ == * ]] ; then
            COMPREPLY=( $(compgen -W "$" -- $) )
            return 0
        fi
    fi
}
complete -o nospace -F _gcd gcd
Share