cd to git_root

Jon Shea May 6th, 2009

When I’m wailing on a project in bash, I frequently find myself wanting to cd back to the project root. Since we use git, this is the same as the git_root directory. So I wrote a bash function that looped through the directories in the current path and found the one that contained the .git/ directory.

Luckily, I thought to ask on #git if there was an easier way before I posted my script (because no one likes to look like a noob on their own blog). It turns out there’s a one liner. I’ve posted it here, with a little padding so that I can feel like I made a contribution.


function gr {
    ## If the current working directory is inside of 
    ## a git repository, this function will change
    ## it to the git root (ie, the directory that 
    ## contains the .git/ directory), and then print
    ## the new directory.    
    git branch > /dev/null 2>&1 || return 1
    cd "$(git rev-parse --show-cdup)".
    pwd
}

Tags: , ,

One Response to “cd to git_root”

  1. {Important|Valuable} {gift|info} for {anybody|anyone} who {needs|wants} {one way backlinks|backlinks} for no {charge|cost}. {Anyone|Anybody} {need|want} free {one way backlinks|backlinks} for their {blog|webite}? I {thought|figured} I {might|would} {share Says:

    Important gift for anybody who needs backlinks for no cost. Anyone want free backlinks for their webite? I thought I might share some good info I discovered a little while ago. Free backlinks for your blog. I have been taking advantage of this this for my blogs and it absolutely works super! Just click my name to get what I mean. Not selling anything, it’s completely free and it works.

Leave a Reply

Subscribe:

Add to Google
RSS
Try ExpanDrive

If you’ve heard of SSH then you need ExpanDrive.