blog icon indicating copy to clipboard operation
blog copied to clipboard

Show git branch in your terminal shell prompt

Open xhlwill opened this issue 9 years ago • 2 comments

vim ~/.bashrc
or vim ~/.bash_profile (Mac OS) and add these:

function find_git_branch {   
    local dir=. head  
    until [ "$dir" -ef / ]; do  
        if [ -f "$dir/.git/HEAD" ]; then  
            head=$(< "$dir/.git/HEAD")  
            if [[ $head == ref:\ refs/heads/* ]]; then  
                git_branch=" ${head#*/*/}"  
            elif [[ $head != '' ]]; then  
                git_branch=' (detached)'  
            else
                git_branch=' (unknown)'
            fi
            return
        fi
        dir="../$dir"
    done
    git_branch=''
}

PROMPT_COMMAND="find_git_branch; $PROMPT_COMMAND"

green=$'\e[1;32m'
magenta=$'\e[1;35m'
normal_colours=$'\e[m'

PS1="\[$green\]\u@\h:\w\[$magenta\]\$git_branch\[$green\]\\$\[$normal_colours\] "

Share and enjoy!
via: http://aaroncrane.co.uk/2009/03/git_branch_prompt/

xhlwill avatar Dec 05 '15 06:12 xhlwill

恩,按照这个弄了一下就好了。

youngwind avatar Dec 05 '15 07:12 youngwind

官方提供了shell,功能更强大,能显示detached分支状态等,做个笔记

xhlwill avatar Dec 22 '16 09:12 xhlwill