1

I'm adding this code

if [ -f ~/.git-prompt.sh ]; then
  source ~/.git-prompt.sh
  export PS1='\[\033[01;34m\]\w\[\033[00m\] $(__git_ps1 "(%s)"): '
fi

to get this result.

~/apps/project-with-git-repo (develop):

But when I go to other folders without a git repository, I see this result.

~/apps/folder-without-git-repo (BARE:master):

How do I get rid of (BARE:master)? For example, if there is no repository, then do not output git at all

2
  • 1
    It's probably easiest to fix this in your ~/.git-prompt.sh ... which you haven't shown. If you really don't want to change that, you can always just use a ${//} substitution or sed to replace that string ... but you also haven't said what you want it replaced with.
    – Useless
    Commented Apr 8, 2022 at 12:07
  • Git itself comes with a rather complicated prompt-setter and other scripts for bash, that does all this for you. If you use that one instead of a private one you won't need to code your own thing, but if you want to use code you write yourself, you need to set PS1 appropriately based on whether you're in a repository in the first place. That is, you must test: "am I in a Git repo" before you attempt to insert the $(__git_ps1 ...) output.
    – torek
    Commented Apr 9, 2022 at 9:54

2 Answers 2

1

I use this:

parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

Followed by setting PS1:

if [ "$color_prompt" = yes ]; then
    PS1="${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w/\[\033[01;33m\]\$(parse_git_branch) \[\033[00m\]\$ "
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
-3

You may try ohmyzsh and enable git plugin

1
  • 2
    The question is about bash.
    – phd
    Commented Apr 8, 2022 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.