基于 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"
希望这会有所帮助,祝您有美好的一天!