Customize colors for __git_ps1 with GIT_PS1_SHOWCOLORHINTS

command-prompt, git, shell

Solution

The colours shown by `__git_ps1` for dirty branches don't affect the branch name; they affect the "dirty state indicator". In addition to enabling colours, if you enable this indicator you will see a red asterisk for a dirty branch:

old-prompt $ bash --noprofile --norc
bash-4.2$ source /etc/bash_completion.d/git-prompt
bash-4.2$ export GIT_PS1_SHOWCOLORHINTS=1
bash-4.2$ export GIT_PS1_SHOWDIRTYSTATE=1
bash-4.2$ export PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\\\$ "'
chris@machine:~/path/to/dir (master *)$

There is no way to change the colour of the branch name based on dirty status without modifying the `git-prompt.sh` code, or providing your own function.

Note that this works with `export PROMPT_COMMAND` but not `export PS1`.

Problem

What I have tried I have updated my prompt to include the branch name using `__git_ps1`. In addition, I set `GIT_PS1_SHOWCOLORHINTS`. The problem The prompt appears correctly. However, the branch color is always green. I expected a dirty branch to be red. The docs state: The colors are based on the colored output of "git status -sb" I found and reviewed How to colorize git-status output? But I'm not sure what options I'd need to change... The question Is it possible to change the branch color to be green for a clean branch and red for a dirty branch? If so, how?

Original source

Related problems