Skip to content

Instantly share code, notes, and snippets.

@benmills
Created December 12, 2012 23:17
Show Gist options
  • Save benmills/4272585 to your computer and use it in GitHub Desktop.
Save benmills/4272585 to your computer and use it in GitHub Desktop.
A better git prompt for Zsh. Include `$(git_prompt)` into your `$PROMPT` to use.
ben@master↑1 S:1 M:2 ?:5
User "ben" on branch "master" ahead by 1 commit with 1 staged change, 3 modified changes and 5 untracked files.
no user@1e1a59
No user configured with commit '1e1a59' checked out.
git_untracked_count() {
count=`echo $(git ls-files --other --exclude-standard | wc -l)`
if [ $count -eq 0 ]; then return; fi
echo " %{$fg_bold[yellow]%}?%{$fg_no_bold[black]%}:%{$reset_color$fg[yellow]%}$count%{$reset_color%}"
}
git_modified_count() {
count=`echo $(git ls-files -md | wc -l)`
if [ $count -eq 0 ]; then return; fi
echo " %{$fg_bold[red]%}M%{$fg_no_bold[black]%}:%{$reset_color$fg[red]%}$count%{$reset_color%}"
}
git_staged_count() {
count=`echo $(git diff-index --cached --name-only HEAD | wc -l)`
if [ $count -eq 0 ]; then return; fi
echo " %{$fg_bold[green]%}S%{$fg_no_bold[black]%}:%{$reset_color$fg[green]%}$count%{$reset_color%}"
}
git_branch() {
branch=$(git symbolic-ref HEAD --quiet 2> /dev/null)
if [ -z $branch ]; then
echo "%{$fg[yellow]%}$(git rev-parse HEAD)#refs/heads/%{$reset_color%}"
else
echo "%{$fg[green]%}${branch#refs/heads/}%{$reset_color%}"
fi
}
git_remote_difference() {
branch=$(git symbolic-ref HEAD --quiet)
if [ -z $branch ]; then return; fi
remote=$(git remote show)
ahead_by=`echo $(git log --oneline $remote/${branch#refs/heads/}..HEAD 2> /dev/null | wc -l)`
behind_by=`echo $(git log --oneline HEAD..$remote/${branch#refs/heads/} 2> /dev/null | wc -l)`
output=""
if [ $ahead_by -gt 0 ]; then output="$output%{$fg_bold[white]%}↑%{$reset_color%}$ahead_by"; fi
if [ $behind_by -gt 0 ]; then output="$output%{$fg_bold[white]%}↓%{$reset_color%}$behind_by"; fi
echo $output
}
git_user() {
user=$(git config user.name)
if [ -z $user ]; then
echo "%{$fg_bold[red]%}no user%{$fg[black]%}@%{$reset_color%}"
else
echo "$user%{$fg[black]%}@%{$reset_color%}"
fi
}
git_prompt_info() {
if [[ ! -d .git ]]; then return; fi
print " $(git_user)$(git_branch)$(git_remote_difference)$(git_staged_count)$(git_modified_count)$(git_untracked_count) "
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment