首页 > 解决方案 > Git 分支名称不会显示在 Mac 终端上

问题描述

我在我的 Mac 中添加了以下几行~/.bash_profile

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

#Variables
userName="\u"
fullPath="\w"
systemName="\h"

#'$(tput setaf 14)' allows coloring for the prompt
#that immediately follows it. 14 is the color aqua.
#A list of colors can be accessed below:
#https://jonasjacek.github.io/colors/

#`$(tput sgr0)` stops the text coloring from continuing on.

#Personalized Terminal
PS1="\[$(tput setaf 118)\]${userName}\[$(tput setaf 3)\]@${systemName}\[$(tput setaf 14)\]${fullPath}\[$(tput setaf 222)\]\[$(parse_git_branch)\]\[$(tput setaf 160)\]$ \[$(tput sgr0)\]"
export PS1;

#export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "

#------- Aliases -------

#Open Sublime Text Editor
alias subl="open -a /Applications/Sublime\ Text.app"
export ANDROID_HOME=/Users/$USER/Library/Android/sdk
export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools

终端样式看起来不错,但我parse_git_branch没有显示我的 git 分支的名称。我在定义值时搞砸了PS1,但我不确定我做错了什么。

标签: bashgit

解决方案


你可以试试这个:

PROMPT_COMMAND=$(cat << 'EOF'
git_prompt=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/')
EOF
)

#Variables
userName="\u"
fullPath="\w"
systemName="\h"

PS1="\[$(tput setaf 118)\]${userName}\[$(tput setaf 3)\]@${systemName}\[$(tput setaf 14)\]${fullPath}\[$(tput setaf 222)\]\[\$git_prompt\]\[$(tput setaf 160)\]$ \[$(tput sgr0)\]"

推荐阅读