在 Shell 提示符中显示 Git 分支名称

| 导语 若是你是用命令行来使用Git的话,当在一个项目中频繁使用多个分支时,能够使用 git status 命令查询本身如今正工做在哪一个分支下面,不过不免有脑子发昏的时候,忘记本身在哪一个分支下面,于是发生误操做之类的杯具。 那么把分支显示在 Shell 提示符中无疑方便了不少,不再须要频繁的使用 git status 命令了…git

 

废话很少,直接上代码,放到 ~/.bash_profile 或者 ~/.profile里便可bash

## 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"

# Hereespa

 
 

black=$'\[\e[1;30m\]'命令行

 
 

red=$'\[\e[1;31m\]'code

 
 

green=$'\[\e[1;32m\]'orm

 
 

yellow=$'\[\e[1;33m\]'blog

 
 

blue=$'\[\e[1;34m\]'it

 
 

magenta=$'\[\e[1;35m\]'class

 
 

cyan=$'\[\e[1;36m\]'file

 
 

white=$'\[\e[1;37m\]'

 
 

normal=$'\[\e[m\]'

 
 

 

 
 

PS1="$white[$magenta\u$white@$green\h$white:$cyan\w$yellow\$git_branch$white]\$ $normal"

相关文章
相关标签/搜索