A Git Directory Aware Shell Prompt

Kevin Altman
Adorable
Published in
2 min readAug 11, 2017

If you’re a developer, like me, you might work in a shell (bash/zsh/fish/etc…) filled world. You might have even customized your shell prompt to make your life easier.

It’s also possible that you use git on a regular basis. Bringing the two of these technologies together is like peanut butter and jelly. They’re made to work together. However, your prompt probably doesn’t respect much about git. It might tell you what branch you’re on, or if you have changes in your repo, but that’s it.

I feel like there’s a lot more valuable information there than just the status of your branch. I offer this to you: How about a prompt that knows when you’re in a git repo, and the directory presented is a reflection of that.

Getting to the point

Ok, the point. You have a git repo at ~/git/my-repo. That directory has many subfolders. When you navigate into that subfolder, you want to know that you’re still in that repo, at a glance, without having to type pwd, and without having the entire absolute path to that directory in your prompt. What if instead, when you went into a subfolder the prompt changed to display my-repo/subfolder1/subfolder2.

It’s possible, and I have the bash and zsh compatible script for you!

Take the git_dir_helper command and insert into your own prompt as you see fit.

I hope you find this helpful, if you’re doing something similar, or have another strategy for this, let me know.

--

--

Kevin Altman
Adorable

Creative Director. Developer. Designer. Lover of humans (and animals) @adorableio. Creator of things you might use http://github.com/itsthatguy