Skip to content

Instantly share code, notes, and snippets.

@johnpbloch
Created August 20, 2012 17:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save johnpbloch/3406013 to your computer and use it in GitHub Desktop.
Save johnpbloch/3406013 to your computer and use it in GitHub Desktop.
Display the current git branch in your command prompt. Add an asterisk if there are unstaged updates.
# Will produce command line prompts like
# directory(branch) $
# in git repositories and like
# directory $
# elsewhere.
# If there are unstaged changes in the repository, there will be an asterisk next to the branch name:
# directory(branch*) $
parse_git_branch() {
hasmod=""
if [[ `git ls-files -dmo --exclude-standard 2> /dev/null` ]]; then
hasmod="*"
fi
git branch 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/(\1$hasmod)/"
}
export PS1="\W\$(parse_git_branch) $ "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment