Rather than roll your own here I'd recommend using what's provided by the Git project out of the box.
On a CentOS 7.x box if you
yum install git everything is already included with Git to do both commandline completion + the git prompt.
$ rpm -ql git|grep completion
Here's the steps to do this manually, they'd need to be added your
~/.bash_profile to make them permanent.
$ . /usr/share/git-core/contrib/completion/git-prompt.sh
$ mkdir somedir && cd somedir
$ git init
$ PS1="$GREEN\t$RED-$BLUE\u$YELLOW\w\[\033[m\]$MAGENTA\$(__git_ps1)$WHITE \$ "
Doing the above will set your prompt so that it goes from this:
13:18:42-root/home/vagrant/somedir (master) $
NOTE: it's now displaying the branch that I'm currently on in this repo, in my prompt.
Where's the colors?
To get this colored you need to define the colors so that the variable
PS1 for the prompt's color variables have corresponding escape codes for their respective colors:
Now reset the
PS1 again to pick these up:
13:24:26-root/home/vagrant/somedir (master) $ PS1="$GREEN\t$RED-$BLUE\u$YELLOW\w\[\033[m\]$MAGENTA\$(__git_ps1)$WHITE \$ "
13:24:35-root/home/vagrant/somedir (master) $
And it'll look like this now:
And what about changing the color of
If is probably something I'd discourage your from because you need to then run a
git status against every directory in each
ls command to determine if it's a Git repo or not.
$ ls somedir
would result in 7
git status commands to determine which directory is a Git repo and which is not, so that you can decide whether to color it as purple or not.