Change to the Top of a Git Repository Quickly

Today I am simply sharing a shortcut for Git that you may find useful if you often run Git from the command-line, as I do. The command git rev-parse can do a lot things, and one of those is showing the top-level directory of any repository. I take advantage of this with a simple function in GNU Bash, my shell of choice, although you could easily write this in any other Unix shell:

function cdtop() {
    pushd "$(git rev-parse --show-toplevel)";

With this command I can now type cdtop from any sub-directory of a Git repository to jump to the top-level. That can be useful when I want to do things like see changes across the whole repository since commands like git status restrict themselves to the current directory by default.

It’s a simple shell function, but maybe you’ll find it useful too.


Add Your Thoughts

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s