There are a lot of blog posts describing techniques of putting your current git
branch in your bash prompt. After reading one of those posts more then year ago
I immediately edited my ~/.bash_profile
and was very happy until very recent,
when I have started using --color
argument with git commands. Surprisingly to
me colors made me so much more effective that I quickly tweaked my git config:
git config --global color.ui auto
This allowed my to have colors everywhere by
default. Shortly after I got an idea that colors in prompt may be used for
displaying a status of the working tree / index.
Motivation
I really wanted to try that idea out, because I thought it was a chance to solve a problem that bugged me for years already. I was looking for a way to remind myself to commit more often, since I tempt to commit only after I’m done with a task and it’s hard to imagine how many times I regretted that!!
Design
So my plan was to display “current git branch” in a bash prompt in a different color depending on a state of the working tree / index:
- No changes
- Untracked files
- Stashed changes
- Modified files
- Deleted files
Implementation
I wont bother you with a details on how to get a “current git branch” since there are lots of posts about it, instead I’ll focus your attention on snippet that parses git status and adds colors:
I am not that good at bash and I would be more then happy to hear your your suggestions how to make implementation smaller or faster. Any other suggestions or comments are also very welcome.
Finally if you want to see full implementation check out my git repo.