首页 > 其他 > 详细

如何在终端实时展现当前运行的git分支

时间:2015-07-01 13:52:01      阅读:202      评论:0      收藏:0      [点我收藏+]

我们在终端操作git的时候,是可以实时的get到当前所在的分支的

操作文件:

~/.bash_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"

# Here is bash color codes you can use
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"

保存后重启shell即可

如何在终端实时展现当前运行的git分支

原文:http://www.cnblogs.com/cuoreqzt/p/4612808.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!