我们在终端操作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\[email protected]$green\h$white:$cyan\w$yellow\$git_branch$white]\$ $normal"
保存后重启shell即可
时间: 2024-10-07 17:16:45