| Introduction If you are using GIT with the command line, you can use the git status command to query which branch you are working on now, but it's hard to forget what branch you're under, so you have a mug with a wrong operation. It is no doubt convenient to display the branch in the Shell prompt, and no longer need to use the git status command very frequently ...
Not much nonsense, directly on the code, put in ~/.bash_profile or ~/.profile can
# # Parses outThe 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)" ElseGit_branch="→ (unknow)"fireturnfi 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 \[email protected] $green \h$white: $cyan \w$yellow\ $git _branch$white]\$ $normal"
Show Git branch name in Shell prompt