Skip to content

Instantly share code, notes, and snippets.

@joonas-yoon
Forked from michaelneu/README.md
Last active April 28, 2021 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joonas-yoon/65b5c34c9f19bc125d6ba442cf957395 to your computer and use it in GitHub Desktop.
Save joonas-yoon/65b5c34c9f19bc125d6ba442cf957395 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.

When adding this script to your .bash_profile or .bashrc, it'll display the selected branch of the current folder (if it's a git repo), and whether it's modified (yellow) or contains staged files (cyan).

example

The script makes the assumption, that a .git folder only exists when the directory is a git repo. Also, it checks for the english version of the git status command, so if you're using git in a different locale, make sure to adjust this.

COLOR_GIT_CLEAN='\[\033[1;30m\]'
COLOR_GIT_MODIFIED='\[\033[0;33m\]'
COLOR_GIT_STAGED='\[\033[0;36m\]'
COLOR_RESET='\[\033[0m\]'
function git_prompt() {
if [ -e ".git" ]; then
branch_name=$(git symbolic-ref -q HEAD)
branch_name=${branch_name##refs/heads/}
branch_name=${branch_name:-HEAD}
echo -n "→ "
if [[ $(git status 2> /dev/null | tail -n1) = *"nothing to commit"* ]]; then
echo -n "$COLOR_GIT_CLEAN$branch_name$COLOR_RESET"
elif [[ $(git status 2> /dev/null | head -n5) = *"Changes to be committed"* ]]; then
echo -n "$COLOR_GIT_STAGED$branch_name$COLOR_RESET"
else
echo -n "$COLOR_GIT_MODIFIED$branch_name*$COLOR_RESET"
fi
echo -n " "
fi
}
function prompt() {
PS1="\e[92m\u\e[90m@\h\e[0m [ \e[92m\w\e[0m $(git_prompt)] \$ "
}
PROMPT_COMMAND=prompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment