Skip to content

Instantly share code, notes, and snippets.

@mattions
Last active December 7, 2020 10:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattions/2051095 to your computer and use it in GitHub Desktop.
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
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\]\$ '
@mattions
Copy link
Author

my prompt looks like

mattions@triton:Neuronvisio(master)$

@liskin
Copy link

liskin commented Apr 19, 2012

Current branch can be determined also using: git symbolic-ref HEAD. Seems a bit cleaner to me. :-)

@mattions
Copy link
Author

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 !

@mattions
Copy link
Author

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