基于6LYTH3的回答,由于一些方便的改进,我决定发布自己的答案:
简单的解决方案
打开~/.bash_profile
并添加以下内容
# \[\e[0m\] resets the color to default color
reset_color='\[\e[0m\]'
# \[\033[33m\] sets the color to yellow
path_color='\[\033[33m\]'
# \e[0;32m\ sets the color to green
git_clean_color='\[\e[0;32m\]'
# \e[0;31m\ sets the color to red
git_dirty_color='\[\e[0;31m\]'
# determines if the git branch you are on is clean or dirty
git_prompt ()
{
# Is this a git directory?
if ! git rev-parse --git-dir > /dev/null 2>&1; then
return 0
fi
# Grab working branch name
git_branch=$(git branch 2>/dev/null| sed -n '/^\*/s/^\* //p')
# Clean or dirty branch
if git diff --quiet 2>/dev/null >&2; then
git_color="${git_clean_color}"
else
git_color="${git_dirty_color}"
fi
echo " [$git_color$git_branch${reset_color}]"
}
export PS1="${path_color}\w\[\e[0m\]$(git_prompt)\n"
这应该:
1) Prompt the path you're in, in color: path_color.
2) Tell you which branch are you.
3) Color the name of the branch based on the status of the branch with git_clean_color
for a clean work directory and git_dirty_color for a dirty one.
4) The brackets should stay in the default color you established in your computer.
5) Puts the prompt in the next line for readability.
您可以使用此列表自定义颜色
精密的解决方案
另一个选择是使用Git Bash Prompt,并与此一起安装。我在Mac OS X上通过Homebrew使用了该选项。
git_prompt_list_themes
看主题,但我不喜欢其中任何一个。
git_prompt_color_samples
查看可用的颜色。
git_prompt_make_custom_theme [<Name of base theme>]
要创建新的自定义主题,应创建一个.git-prompt-colors.sh文件。
subl ~/.git-prompt-colors.sh
打开git-prompt-colors.sh并自定义:
我自定义的.git-prompt-colors.sh文件应如下所示
override_git_prompt_colors() {
GIT_PROMPT_THEME_NAME="Custom"
# Clean or dirty branch
if git diff --quiet 2>/dev/null >&2; then
GIT_PROMPT_BRANCH="${Green}"
else
GIT_PROMPT_BRANCH="${Red}"
fi
}
reload_git_prompt_colors "Custom"
希望这会有所帮助,祝您有美好的一天!
bash: parse_git_branch: command not found