show git branch bash prompt

780 ワード

Paste the following code into the file, and save it.
parse_git_branch () {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

parse_git_tag () {
  git describe --tags 2> /dev/null
}

parse_git_branch_or_tag() {
  local OUT="$(parse_git_branch)"
  if [ "$OUT" == " ((no branch))" ]; then
    OUT="($(parse_git_tag))";
  fi
  echo $OUT
}

Edit your  .bashrc  or  .bash_profile  file to override the PS1 value.
vim ~/.bashrc

Add the following code at the bottom of the file, and save it.
source ~/.bash/git-prompt
 PS1="\u@\h:\w\$(parse_git_branch_or_tag) $ "

Restart your Terminal.