Skip to content

Instantly share code, notes, and snippets.

@electron0zero
Forked from johnpbloch/.bashrc
Last active January 5, 2024 05:42
Show Gist options
  • Save electron0zero/7685fca7c2ae4d0cd355aa670675b65f to your computer and use it in GitHub Desktop.
Save electron0zero/7685fca7c2ae4d0cd355aa670675b65f to your computer and use it in GitHub Desktop.
Display the current git branch in your command prompt with username and dir., Adds an asterisk if there are unstaged updates.
# Add this to your .bashrc
# Will produce command line prompts like in git repositories
# <username>@<hostname>:<directory>(branch) $
# and like this elsewhere.
# <username>@<hostname>:<directory> $
# If there are unstaged changes in the repository, there will be an asterisk next to the branch name:
# <username>@<hostname>:<directory>(branch) $
# taken from https://gist.github.com/johnpbloch/3406013
# Trim path https://unix.stackexchange.com/a/26950/270668
export PROMPT_DIRTRIM=1
parse_git_branch() {
hasmod=""
if [[ $(git ls-files -dmo --exclude-standard 2> /dev/null) ]]; then
hasmod="*"
fi
git branch 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \\(.*\\)/(\\1$hasmod)/"
}
export PS1="\[\e[46m\]\[\e[30m\]\\u@\\h\[\e[01;00m\]:\[\e[43m\]\[\e[30m\]\\w\[\e[42m\]\[\e[30m\]\$(parse_git_branch)\[\e[01;00m\] $ "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment