Add git branch name and last commit to bash prompt
If you want see current git branch in terminal you can use bash prompt feature.
By default bash has PROMPT_COMMAND variable. The contents of this variable are executed as a regular Bash command just before Bash displays a prompt. Using this variable you can request current git branch and commit and display it in terminal.
By default bash has PROMPT_COMMAND variable. The contents of this variable are executed as a regular Bash command just before Bash displays a prompt. Using this variable you can request current git branch and commit and display it in terminal.
echo_c() { w=$(stty size | cut -d" " -f2) # width of the terminal l=${#1} # length of the string printf "%"$((l+(w-l)/2))"s\n" "$1" # print string padded to proper width (%Ws) } change_prompt() { history -a local BLACK="\[\033[0;30m\]" local BLACKBOLD="\[\033[1;30m\]" local RED="\[\033[0;31m\]" local REDBOLD="\[\033[1;31m\]" local GREEN="\[\033[0;32m\]" local GREENBOLD="\[\033[1;32m\]" local YELLOW="\[\033[0;33m\]" local YELLOWBOLD="\[\033[1;33m\]" local BLUE="\[\033[0;34m\]" local BLUEBOLD="\[\033[1;34m\]" local PURPLE="\[\033[0;35m\]" local PURPLEBOLD="\[\033[1;35m\]" local CYAN="\[\033[0;36m\]" local CYANBOLD="\[\033[1;36m\]" local WHITE="\[\033[0;37m\]" local WHITEBOLD="\[\033[1;37m\]" if [[ -d .git ]]; then GIT_BRANCH=`git branch | grep "\*" | sed "s/\* //g"` GIT_BRANCH=$GIT_BRANCH' - '`git rev-parse --short HEAD` PS1='$GIT_BRANCH' PS1=`echo_c $PS1` PS1=$GREEN$PS1'\n'$WHITEBOLD'\t - (${debian_chroot:+($debian_chroot)}'$WHITE'\w)\$\[\e[0m\] ' return 0 fi GIT_BRANCH="" PS1='\[\033[1;37m\]\t - (${debian_chroot:+($debian_chroot)}\[\033[1;37m\]\w)\$\[\e[0m\] ' } PROMPT_COMMAND='change_prompt'
After restarting your terminal it should look like this:
Bash will show branch and last commit at the middle of terminal only if you are in the folder that is git repository. You can change color of text substituting color variables. For example if you are using terminal with white background, substitute all WHITE and WHITEBOLD.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home