Tidbits | Nov. 8, 2022

Pro-Tip – Helpful Shell alias to git repo root

by Frank Wiles |   More posts by Frank

I've recently been on a mission to take my own advice and spend a little bit of time each week optimizing my personal coding setup. Recently I constantly need to got to the root of the current git repository I'm in.

It doesn't sound like a big deal, does it?

It isn't that big of a deal. However, it's keeping me from dozens to hundreds of `cd ../../..` type keystrokes every day! This won't single-handedly keep RSI away, but every little bit helps.

So what did I set up? I set up two zsh aliases.

  • `r` which will immediately cd me to the root of the current git repository I'm in
  • `rd` which will run the fd command from the perspective of the root of the git repo

I'm on OSX, so the only new dependency was I needed to install `git-extras` with `brew install git-extras`. git-extras is useful all on it's own, but I haven't used that many of its features yet. My muscle memory for such things takes awhile to set in as I'm sure it does for you.

I added the following to the end of my `~/.zshrc`:

# Git customization
alias r='cd $(git root)'
function rd() {
    fd "$1" $(git root)

Now, if I type `r` I'm immediately taken to the git root. And if I run `rd settings` from a couple of directories deep in a code base I get.

git zsh rd function shell output

I hope you'll join me in taking a little time to improve our workflows for better developer experience (DX) and a slightly easier day!

Fish Shell Version

Daniel worked up the fish shell version for us all

# ~/fish-configs/functions/r.fish
function r
    cd (git root)

function rd -a pattern
    fd "$pattern" (git root)

git   zsh   dx  

I created a simple `r` shell alias that will take me to the root of the current git repository. As this is a common thing I need to do on my Mac BookPro when I've been exploring deep into a code base and now need to run some command at the root.{% else %}

2022-11-08T16:43:00 2022-11-09T08:48:29.171304 2022 git,zsh,dx