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

我们在终端操作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[$magentau$white@$greenh$white:$cyanw$yellow$git_branch$white]$ $normal"

保存后重启shell即可

原文地址:https://www.cnblogs.com/cuoreqzt/p/4612808.html