Home > database >  How to display the git branch of the current repository when customizing bash?
How to display the git branch of the current repository when customizing bash?

Time:04-09

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

CodePudding user response:

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

CodePudding user response:

You may try ohmyzsh and enable git plugin

  • Related