Skip to content

Instantly share code, notes, and snippets.

@StephenHwang
Created September 9, 2022 21:34
Show Gist options
  • Save StephenHwang/1addd7a57618aa56715fb439e2123852 to your computer and use it in GitHub Desktop.
Save StephenHwang/1addd7a57618aa56715fb439e2123852 to your computer and use it in GitHub Desktop.
Git branch in Bash prompt

I found it helpful to see the Github branch I am working on in the Bash prompt.

Adding the following snippet into the .bashrc allows for a colored bash prompt that parses and includes the git branch and whether there are uncommitted changes in the directory. Uncommitted changes are specified by the inclusion of a * after the git branch. For larger git branches, tracking file modification may be slow. Comment the MODIFIED line to not check for uncommitted file modifications.

Add this snippet to your .bashrc:

parse_git_branch() {
  MODIFIED=$(git status 2> /dev/null | grep 'Changes not staged for commit' >/dev/null && echo \*)
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1'"$MODIFIED"')/'
}
export PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[33m\]$(parse_git_branch)\[\033[00m\]$ '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment