mac bash上顯示git分支與狀態

 

主要實現javascript

  • 顯示當前路徑
  • 顯示當前所在分支
  • 顯示當前修改狀態
    • = 表示一個乾淨的分支
    • ~ 表示文件有改動
    • # 表示已commit 但未 push

 

經過網上搜索和本身根據實際須要修改的代碼以下:java

.bash_profilegit

function parse_git_dirty {
    local git_status=$(git status 2> /dev/null | tail -n1) || $(git status 2> /dev/null | head -n 2 | tail -n1);
    local git_now;
    if [[ "$git_status" != "" ]]; then
        #local git_now; # 標示
	#echo "${git_status}"
        if [[ "$git_status" =~ nothing\ to\ commit || "$git_status" =~  Your\ branch\ is\ up\-to\-date\ with ]]; then
            git_now="=";
        elif [[ "$git_status" =~ no\ changes\ added\ to\ commit ]]; 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\ up\-to\-date\ with ]]; then
        #    git_now="#";
        fi
       # echo "${git_now}";
    elif [[ "$git_status" = "" ]]; then
	git_now="#";
    fi
   echo "${git_now}"
}


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\]$ "
相關文章
相關標籤/搜索