Skip to content

Instantly share code, notes, and snippets.

@artm
Forked from michaelneu/README.md
Last active December 19, 2022 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save artm/27256197e6f17cf8e0207577e3c09287 to your computer and use it in GitHub Desktop.
Save artm/27256197e6f17cf8e0207577e3c09287 to your computer and use it in GitHub Desktop.
A basic, but fast git prompt.

bash-basic-git-prompt

This is a considerably faster, but much more basic alternative to bash-git-prompt.

Thanks to @michaelneu for inspiration. I modified the script to make it even faster on windows, where subshells are too slow. I also inserted a new line into the prompt as personal preference.

screenshot

COLOR_GIT_CLEAN='\[\e[1;30m\]'
COLOR_GIT_MODIFIED='\[\e[1;31m\]'
COLOR_RESET='\[\e[0m\]'
COLOR_GREEN='\[\e[0;32m\]'
COLOR_YELLOW='\[\e[0;33m\]'
function append_git_prompt() {
declare -A counts
while read -a l ; do
local tag="${l[0]}"
[[ $tag = "fatal:" ]] && return
[[ $tag = "??" ]] && tag="U"
counts[$tag]=$((${counts[$tag]:-0} + 1))
done < <(git status --porcelain 2>&1)
local branch_name=$(git symbolic-ref -q HEAD)
branch_name=${branch_name##refs/heads/}
branch_name=${branch_name:-HEAD}
PS1+="→ "
if [[ "${#counts[@]}" = "0" ]] ; then
PS1+="${COLOR_GIT_CLEAN}"
else
PS1+="${COLOR_GIT_MODIFIED}"
fi
PS1+="$branch_name${COLOR_RESET}"
[[ ${counts[M]} -gt 0 ]] && PS1+=" *${counts[M]}"
[[ ${counts[A]} -gt 0 ]] && PS1+=" +${counts[A]}"
[[ ${counts[U]} -gt 0 ]] && PS1+=" ?${counts[U]}"
PS1+=" "
}
function prompt() {
PS1="${COLOR_GREEN}\u@\h${COLOR_RESET} [ ${COLOR_YELLOW}\w${COLOR_RESET} "
append_git_prompt
PS1+="]\n\$ "
}
PROMPT_COMMAND=prompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment