chiawenwang / blog

Recording the errors
0 stars 0 forks source link

Show git branch in terminal #9

Closed chiawenwang closed 6 months ago

chiawenwang commented 6 months ago

对于 Bash shell

在你的 ~/.bashrc 文件中添加以下行:

export PS1='[\033[36m]\u@\h[\033[m]:[\033[32m]\w[\033[m]:[\033[31m]$(__git_ps1)[\033[m] '

保存文件并重新加载配置,使用命令:

source ~/.bashrc

chiawenwang commented 6 months ago

完整如下:

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@\h \[\033[01;36m\]\W\[\033[01;32m\]$(git_branch)\[\033[00m\] \$ '