Git branch name is not updated on terminal prompt when checking out another git branch

Posted on

Problem :

I’m having this command to set prompt in Bash terminal with Git branch name:

export PS1="w $(git branch | grep '*') "

It shows the correct Git branch name for the first time after running the export command above. However, when checking out another Git branch, the prompt doesn’t change to show new Git branch name.

I guess the subbash command $(…) is executed only once, how to make it run every time the prompt is shown after pressing Enter?

Solution :

You need to set PROMPT_COMMAND. First, define a function that will
run every time a new prompt is shown:

function setps1()
    PS1="w $(git branch | grep '*') "

and assign it to PROMPT_COMMAND:


Also check out projects such as

Leave a Reply

Your email address will not be published. Required fields are marked *