I had to use PROMPT_COMMAND, setting PROMPT_COMMAND to a function that changes PS1. And I had to use the condition git rev-parse --is-inside-work-tree > /dev/null 2>&1 to validate whether it is a git repository or not.
PROMPT_COMMAND
git rev-parse --is-inside-work-tree > /dev/null 2>&1