Skip to content

Instantly share code, notes, and snippets.

@indirect
Created October 18, 2010 02:49
Show Gist options
  • Save indirect/631628 to your computer and use it in GitHub Desktop.
Save indirect/631628 to your computer and use it in GitHub Desktop.
improved git prompt yay
# [andre ~/.bash](master)$ # clean working directory
# [andre ~/.bash](master⚡)$ # dirty working directory
# [andre ~/.bash](master~2)$ # checked out revision not a branch
# [andre ~/.bash](branch)$ # checked out branch with same commits as master
function parse_git_dirty {
[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "⚡"
}
function parse_git_branch {
local b="$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/^* //')"
if [ -n "$b" ] && [ "$b" = "(no branch)" ]; then
local b="$(git name-rev --name-only HEAD 2> /dev/null)"
fi
if [ -n "$b" ]; then
printf "($b$(parse_git_dirty))"
fi
}
export PS1='\[\033k\033\\\]\[\e[0;34m\][\u \w]$(parse_git_branch)\$\[\e[0;39m\] '
@indirect
Copy link
Author

Now actually working. Heh.

@indirect
Copy link
Author

Now supporting branch names even when the checked out branch has no commits past master, woo.

@EmmanuelOga
Copy link

You may already know about these:

export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_SHOWSTASHSTATE=1
export GIT_PS1_SHOWUNTRACKEDFILES=1
PS1="$(__git_ps1)\n▸ "

Although the dirty state chars might not be as fancy as yours :p

@indirect
Copy link
Author

The main reason I wrote my own is that __git_ps1 doesn't support non-branch locations, like master~2. If that's changed, I'm a happy man. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment