Skip to content

Instantly share code, notes, and snippets.

@lillesvin
Last active August 29, 2015 14:22
Show Gist options
  • Save lillesvin/5ca9a23640767b81389d to your computer and use it in GitHub Desktop.
Save lillesvin/5ca9a23640767b81389d to your computer and use it in GitHub Desktop.
Show active git branch in Bash prompt
#!/bin/bash
# Put this in $HOME/bin/get_git_branch.sh
if branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null); then
if [[ "$branch" == "HEAD" ]]; then
branch='detached*'
fi
fi
echo $branch
# Add this to $HOME/.bashrc
function git_prompt {
gitBranch=`$HOME/bin/get_git_branch.sh`
if [ -z $gitBranch ]; then
PS1='\u@\h:\W\$ '
else
PS1='\u@\h:\W (\[\033[0;35m\]$gitBranch\[\033[0m\])\$ '
fi
}
PROMPT_COMMAND=git_prompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment