Last active
December 7, 2020 10:54
-
-
Save mattions/2051095 to your computer and use it in GitHub Desktop.
parse_git function in bash which updates your prompt with the git branch if it is a git repo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parse_git_dirty { | |
[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit, working tree clean" ]] && echo "*" | |
} | |
function parse_git_branch { | |
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/(\1$(parse_git_dirty))/" | |
} | |
export PS1='\u@\h \[\033[1;33m\]\w\[\033[0m\]$(parse_git_branch)$ ' | |
#grey | |
PS1='\[\033[0;32m\]\u\[\033[1;32m\]@\[\033[0;32m\]\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[1;30m\]$(parse_git_branch)\[\033[00m\]\$ ' |
Current branch can be determined also using: git symbolic-ref HEAD. Seems a bit cleaner to me. :-)
Yeah, that could be an option as well!
TBH, I hacked this long time ago from pieces found on Internet and tries and error with the regex !
Note:
On the last version of git (git version 1.8.1.2) you need to change the line:
"nothing to commit (working directory clean)"
to
"nothing to commit, working directory clean"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
my prompt looks like