在 Shell 提示符中显示 Git 分支名称
| 导语 如果你是用命令行来使用Git的话,当在一个项目中频繁使用多个分支时,可以使用 git status 命令查询自己现在正工作在哪个分支下面,不过难免有脑子发昏的时候,忘记自己在哪个分支下面,因而发生误操作之类的杯具。 那么把分支显示在 Shell 提示符中无疑方便了很多,再也不需要频繁的使用 git status 命令了…
废话不多,直接上代码,放到 ~/.bash_profile 或者 ~/.profile里即可
## Parses out the branch name from .git/HEAD: 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=" → (unknow)" fi return fi dir="../$dir" done git_branch=\'\' } PROMPT_COMMAND="find_git_branch; $PROMPT_COMMAND"
# Heree
black=$\’\[\e[1;30m\]\’
red=$\’\[\e[1;31m\]\’
green=$\’\[\e[1;32m\]\’
yellow=$\’\[\e[1;33m\]\’
blue=$\’\[\e[1;34m\]\’
magenta=$\’\[\e[1;35m\]\’
cyan=$\’\[\e[1;36m\]\’
white=$\’\[\e[1;37m\]\’
normal=$\’\[\e[m\]\’
PS1=“$white[$magenta\u$white@$green\h$white:$cyan\w$yellow\$git_branch$white]\$ $normal”