Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show git branch in your terminal shell prompt #1

Open
xhlwill opened this issue Dec 5, 2015 · 2 comments
Open

Show git branch in your terminal shell prompt #1

xhlwill opened this issue Dec 5, 2015 · 2 comments
Labels

Comments

@xhlwill
Copy link
Owner

xhlwill commented Dec 5, 2015

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/

@youngwind
Copy link

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

@xhlwill
Copy link
Owner Author

xhlwill commented Dec 22, 2016

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

@xhlwill xhlwill changed the title Show git branch in your shell prompt Show git branch in your terminal shell prompt Dec 22, 2016
@xhlwill xhlwill added the Git label May 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants