Skip to content

Instantly share code, notes, and snippets.

@imolein
Last active April 12, 2019 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imolein/7c379026520f32eff9f8ac77d2d865e7 to your computer and use it in GitHub Desktop.
Save imolein/7c379026520f32eff9f8ac77d2d865e7 to your computer and use it in GitHub Desktop.
Bash prompt with git branch and dirty detection
# Add the lines to your .bashrc
# color definitions
r_red="\e[0;31m"
r_grn="\e[0;32m"
b_wht="\e[1;37m"
b_cyn="\e[1;36m"
b_red="\e[1;31m"
b_blu="\e[1;34m"
b_ylw="\e[1;33m"
b_pur="\e[1;35m"
rst="\e[0m"
# check if git is dirty
function is_git_dirty() {
local status=$(git status --porcelain 2> /dev/null)
if [ "$status" != "" ]; then
echo "*"
else
echo ""
fi
}
# get current branch
function get_git_branch() {
local branch=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
local dirty
if [ ! "${branch}" == "" ]; then
dirty=`is_git_dirty`
echo -e "${b_wht}[${r_grn}${branch}${r_red}${dirty}${b_wht}]"
else
echo ""
fi
}
export PS1="\[${b_wht}\][ \[${b_red}\]\u\[${b_ylw}\]@\[${b_cyn}\]\h \[${b_ylw}\]\w \`get_git_branch\` \[${b_wht}\]] \[${b_pur}\]#\[${rst}\]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment