git: a quick command to go to root of the working tree

bash, git, scripting

Solution

This has been asked before, Is there a way to get the git root directory in one command? Copying @docgnome's answer, he writes

cd $(git rev-parse --show-cdup)

Make an alias if you like:

alias git-root='cd $(git rev-parse --show-cdup)'

Problem

I wanted a simple git command to go up to the "root" of the repository. I started with a script, but figured that I cannot change active directory of the shell, I had to do a function. Unfortunately, I cannot call it directly with the non-dash form "git root", for instance. ``` function git-root() { if [ -d .git ]; then return 0 fi A=.. while ! [ -d $A/.git ]; do A="$A/.." done cd $A } ``` Do you have a better solution? (the function has been written quickly, suggestions are welcome)

Original source

Related problems