To include your git branch in your bash shell prompt, add this to your .bashrc
and start a new shell window.
function gitrepo(){
repo=$(git branch 2> /dev/null | gawk 'NF==2 {print $2}')
if [ -n "$repo" ]; then
echo "($repo) "
fi
}
PS1="\u@\h:\w \$(gitrepo)\$ "
This is what you end up with.
azemon@xps13:~/myapp (BA-2739) $
The git branch only shows up when you are in a directory that is part of a git repository.
This implementation relies on gawk. If you do not have gawk installed, you can either install it (something like “apt install gawk”) or do your own implementation of gitrepo based on something else, perhaps grep and sed. “This is left as an exercise for the student.”