Mac在使用终端的时候,默认git没有显示项目的分支名字,而mac的shell又分好多种,我目前有使用过bash和zsh,所以就这2种shell怎么显示git分支名记录一下。
# 一. bash
> vim ~/.bash_profile'
编辑这个文件,在里面添加
```bash
function git_branch {
branch="`git branch 2>/dev/null | grep "^\*" | sed -e "s/^\*\ //"`"
if [ "${branch}" != "" ];then
if [ "${branch}" = "(no branch)" ];then
branch="(`git rev-parse --short HEAD`...)"
fi
echo " ($branch)"
fi
}
export PS1='\u \w\[\033[01;32m\]$(git_branch)\[\033[00m\] \$ '
```
然后使生效执行source ~/.bash_profile,然后再 vim ~/.zshrc, 添加source ~/.bash_profile
另有人编辑以上文件,添加一下
```bash
funcation parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "
```
这个没有试过,看有人这样说也可以,仅供参考。
# 二. zsh
> vim ~/.zshrc
编辑这个文件,在里面添加
```bash
function parse_git_branch() {
git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}
setopt PROMPT_SUBST
export PROMPT='%F{grey}%n%f %F{cyan}%~%f %F{green}$(parse_git_branch)%f %F{normal}$%f '
```
然后使生效执行source ~/.zshrc
Mac终端git显示分支名记录