Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.