1

I'm trying to add my current git branch to my bash terminal prompt. I've copied this file: https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh to my home directory and this is the part of .bashrc that relates to my prompt:

source ~/.git-prompt.sh

if [ "$color_prompt" = yes ]; then
        PS1='${debian_chroot:+($debian_chroot)}\[\033[32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00;31m\] \W$(__git_ps1 " (%s)")\[\033[00m\]\$ '
else
        PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\ \W$(__git_ps1 " (%s)")$ '
fi

However, \W$(__git_ps1 " (%s)") is returning the current folder and the branch, and returns the current directory when not in a repository.

Is it possible to get it to only display the current branch when in a repository and when I'm not in one, display nothing?

  • To encourage you in the right direction: Your question was well posted, documented and so, but it was unfortunately a duplicate. – Léa Gris Aug 27 '20 at 21:40

1 Answers1

1

I've found that the \W was inserting the current directory which wasn't commented on in the source code or anywhere where people were talking about implementations. I've fixed it to this:

PS1='${debian_chroot:+($debian_chroot)}\[\033[32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00;31m\]$(__git_ps1 " (%s)")\[\033[00m\]\$ '
  • Would have been maybe a better choice to discuss and provide a solution for this into https://stackoverflow.com/questions/15883416/adding-git-branch-on-the-bash-command-prompt – Léa Gris Aug 27 '20 at 20:35