Skip to content

Instantly share code, notes, and snippets.

@ronshapiro
Created June 26, 2012 05:14
Show Gist options
  • Save ronshapiro/2993473 to your computer and use it in GitHub Desktop.
Save ronshapiro/2993473 to your computer and use it in GitHub Desktop.
Git command line branch warning
# Add this to your .bash_profile
# Use this to highlight the current git branch name in your bash prompt - very useful for warning you about git branches that you should not be editing locally
# to add a warning for a branch, do `git config --local --add branch.BRANCHNAME.editwarning true` and unset with `git config --local --unset branch.BRANCHNAME.editwarning
# Colors can be found at https://wiki.archlinux.org/index.php/Color_Bash_Prompt
# thanks to @shreyansb - https://github.com/shreyansb/dotfiles/blob/master/bash/bash_profile for the idea
# and https://gist.github.com/31967 for PROMPT_COMMAND
#PROMPT_COMMAND tells bash to call format_PS1 every time prompt is requested,
#which will set PS1
PROMPT_COMMAND=format_PS1
function format_PS1() {
git_branch="$(__git_ps1 "%s")"
output="${git_branch}" #output will be changed but you want to save git_branch for later
if [[ -z $git_branch ]]; then
output=""
#bplock the dotfiles repo from showing the branch
elif [[ $git_branch == "master-dotfiles" ]]; then
output=""
else
status="$(git status 2>/dev/null)"
# to add a warning for a branch, do `git config --local --add branch.BRANCHNAME.editwarning true` and unset with `git config --local --unset branch.BRANCHNAME.editwarning
branch_warning="branch.${git_branch}.editwarning=true"
hide="$(git config -l)"
if [[ $hide =~ $branch_warning ]]; then
output="\[${bakred}\] -- ${output} -- \[${txtend}\]"
else
output="(${output})"
fi
if [[ $status =~ "Untracked files" ]]; then
output="\[${txtylw}\]${output}\[${txtend}\]"
elif [[ $status =~ "Changes not staged for commit" ]]; then
output="\[${txtylw}\]${output}\[${txtend}\]"
export PS1="\[${undgrn}\]\w\[\e[m\]$output \$ "
}
# You'll probably also want to install bash-completion with homebrew:
if which brew > /dev/null; then
export PATH=/usr/local/bin:$PATH
# running `brew --prefix` twice can actually take considerable time
# every time you open a new shell. I recommend replacing both calls
# with /usr/local (or wherever you changed your brew prefix to)
# to make it faster
if [ -f `brew --prefix`/etc/bash_completion ]; then
. `brew --prefix`/etc/bash_completion
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment