Skip to content

Instantly share code, notes, and snippets.

@chrisnolet
Forked from henrik/.bashrc
Last active September 27, 2022 13:37
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save chrisnolet/46206e21d59f8250bb979a8516a9a034 to your computer and use it in GitHub Desktop.
Save chrisnolet/46206e21d59f8250bb979a8516a9a034 to your computer and use it in GitHub Desktop.
Color-coded git branch for bash prompt
git_branch() {
local branch=$(git --no-optional-locks branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/^* (*\([^)]*\))*/\1/')
if [[ -n $branch ]]; then
if [[ -z $(git --no-optional-locks status --porcelain 2> /dev/null) ]]; then
echo -e " \001\033[32m\002($branch)\001\033[0m\002"
else
echo -e " \001\033[31m\002($branch)\001\033[0m\002"
fi
fi
}
export PS1="\h:\W\$(git_branch) \u$ "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment