Search code examples
gitbashgit-bashbash-completion

make bash completion reset on branch switch


I have the following problem:

I have set up that the branch I currently reside in, is shown in my command prompt like this:

"[Current_working_dir] (master)"

however every time I switch branch I get the following error and the branch isn't refreshed:

[work_environment] (master) git checkout test 
M   bash/bashrc
Switched to branch 'test'
[work_environment] (master)

My .bashrc looks like the following:

export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"

# Bash git completion
source /etc/bash_completion.d/git-prompt

#Colored command prompt + minimum view
export PS1="\[\e[0;34m\][\[\e[1;34m\]\W\\[\e[0;34m\]]\[\e[0m\]\[\e[0;32m\]$(__git_ps1)\[\e[0m\] "

could anyone enlighten me about what i'm doing wrong? I cannot seem to find anything regarding this error


Solution

  • Your problem is the double quotes on this line:

    export PS1="\[\e[0;34m\][\[\e[1;34m\]\W\\[\e[0;34m\]]\[\e[0m\]\[\e[0;32m\]$(__git_ps1)\[\e[0m\] "
    

    That line is being evaluated immediately and __git_ps1 is being called only once.

    Try echo "$PS1" to see what I mean.

    You want single quotes there. The value of PS1 is evaluated before it is used so the function will get called correctly at that point (and see the current branch/etc.).

    export PS1='\[\e[0;34m\][\[\e[1;34m\]\W\\[\e[0;34m\]]\[\e[0m\]\[\e[0;32m\]$(__git_ps1)\[\e[0m\] '