Skip to content

Instantly share code, notes, and snippets.

@noahlt
Created October 23, 2011 07:24
Show Gist options
  • Save noahlt/1306989 to your computer and use it in GitHub Desktop.
Save noahlt/1306989 to your computer and use it in GitHub Desktop.
How can I quickly switch to the topmost directory in a git project?
function rcd {
prevdir=`pwd`
until ls -ld .git 1>/dev/null 2>/dev/null
do
cd ..
if [ `pwd` == '/' ]
then
cd $prevdir
return
fi
done
}

Sometimes I'm deeply nested in the directory structure of a git repo and I want to quickly switch to the topmost directory (you know, the one that has a .git subdirectory).

Put this in your .bashrc, run source .bashrc, then run it:

~/project_name/public/stylesheets/widgets $ rcd
~/project_name $ 
["bash","git","cd"]
@ericl
Copy link

ericl commented Mar 22, 2012

cd git rev-parse --show-toplevel # includes helpful error message

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment