Git: Branch Name on bash prompt
I use git through the command line, so I find it very useful to be able to see what branch I’m on instantly, when I enter a git project directory.
Since PS1
is highly configurable, one can add the git branch to the prompt line, which saves us one git branch
call every time.
Getting the current git branch
There is quite a recent command to get the current branch name when in a git directory:
However, if we’re not in a directory, where we have git sources, an error will pop up:
It’s pretty reasonable, but the problem here is that if we use it in the prompt, it may show up whenever we navigate to a non-git folder.
Luckily, since it’s an error, it shows up on STDERR, therefore we can just redirect the stream to /dev/null
.
My prompt
The prompt that I use on my Linux boxes looks like that. I like things in colour - it’s more clear what is what. The branch name shows up only if I’m in a git project directory. Otherwise it’s just blank space.