Skip to content

Instantly share code, notes, and snippets.

@michaelneu
Last active December 29, 2023 04:06
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save michaelneu/943693f46f7aa249fad2e6841cd918d5 to your computer and use it in GitHub Desktop.
Save michaelneu/943693f46f7aa249fad2e6841cd918d5 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="\u@\h [ \w $(git_prompt)] \$ "
}
PROMPT_COMMAND=prompt
@colegatron
Copy link

Added you code to my own kubernetes and docker prompt:
https://colegatron.medium.com/bash-prompt-for-kubernetes-docker-and-git-a089a07eb3b2

@dixtel
Copy link

dixtel commented Mar 15, 2022

For now this script only shows git prompt on root level of git repository. To change that you need to modify line 7:
if [ -e ".git" ]; then
to:
if git rev-parse --is-inside-work-tree > /dev/null 2>&1; then

@colegatron
Copy link

You are absolutely right. I usually run all my stuff and scripts from the root of the projects, and didn't had to worry about, but your addition is very welcome.
Already updated the gist.
Thank you

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