Skip to content

Instantly share code, notes, and snippets.

@noyobo
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noyobo/968ec2650386b59b4638 to your computer and use it in GitHub Desktop.
Save noyobo/968ec2650386b59b4638 to your computer and use it in GitHub Desktop.
mac bash 显示Git当前分支名与状态
function parse_git_dirty {
local git_status=$(git status 2> /dev/null | tail -n1) || $(git status 2> /dev/null | head -n 2 | tail -n1);
if [[ "$git_status" != "" ]]; then
local git_now; # 标示
if [[ "$git_status" =~ nothing\ to\ commit || "$git_status" =~ Your\ branch\ is\ up\-to\-date\ with ]]; then
git_now="=";
elif [[ "$git_status" =~ Changes\ not\ staged || "$git_status" =~ no\ changes\ added ]]; then
git_now='~';
elif [[ "$git_status" =~ Changes\ to\ be\ committed ]]; then #Changes to be committed
git_now='*';
elif [[ "$git_status" =~ Untracked\ files ]]; then
git_now="+";
elif [[ "$git_status" =~ Your\ branch\ is\ ahead ]]; then
git_now="#";
fi
echo "${git_now}";
fi
}
function git_branch {
ref=$(git symbolic-ref HEAD 2> /dev/null) || return;
echo "("${ref#refs/heads/}") ";
}
PS1="[\[\033[1;32m\]\w\[\033[0m\]] \[\033[0m\]\[\033[1;36m\]\$(git_branch)\[\033[0;31m\]\$(parse_git_dirty)\[\033[0m\]$ "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment