Skip to content

Instantly share code, notes, and snippets.

@greathmaster
Last active March 9, 2023 16:41
Show Gist options
  • Save greathmaster/f25c461d64e48d25735d738cddd83569 to your computer and use it in GitHub Desktop.
Save greathmaster/f25c461d64e48d25735d738cddd83569 to your computer and use it in GitHub Desktop.
Prompt for zshell to show git branch and current directory
# Set color codes for directory and Git branch
dir_color='%F{blue}'
branch_color='%F{green}'
# Set Git branch state color codes
uncommitted_color='%F{red}'
untracked_color='%F{yellow}'
ahead_color='%F{cyan}'
behind_color='%F{magenta}'
conflicted_color='%F{red}'
# Set zshell prompt to show directory and Git branch with state
setopt PROMPT_SUBST
PROMPT='${dir_color}%1d${reset_color}${${$(git rev-parse --abbrev-ref HEAD 2>/dev/null)#(refs/heads/|stash|worktree/)#}:+ (${branch_color}%1:s${reset_color}${(color$(
if [[ $(git status --porcelain --untracked-files=all) == *M* ]]; then
echo $uncommitted_color
elif [[ $(git status --porcelain --untracked-files=all) == *\\?\\?* ]]; then
echo $untracked_color
elif [[ $(git rev-list --count --left-right @{u}...HEAD) == *"<"* ]]; then
echo $ahead_color
elif [[ $(git rev-list --count --left-right @{u}...HEAD) == *">"* ]]; then
echo $behind_color
elif [[ $(git status --porcelain) == *"U"* ]]; then
echo $conflicted_color
else
echo ''
fi))}${reset_color}\$ '
# Set color code reset
reset_color='%f'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment