Skip to content

Instantly share code, notes, and snippets.

@maewolfsky
Last active August 29, 2015 14:23
Show Gist options
  • Save maewolfsky/5f67fa8b19147a788f7c to your computer and use it in GitHub Desktop.
Save maewolfsky/5f67fa8b19147a788f7c to your computer and use it in GitHub Desktop.
Show your current git branch in your prompt

Description

I find it helpful to be able to see what branch you have checked out when inside a git repo. Just make the following additions to your .bashrc (or any other file that gets sourced on shell creation).

.bashrc

function parse_git_remote_origin {
  git config --get remote.origin.url | sed -Ee "s/^(https:\/\/|git@|ssh:\/\/git@)?([^:\/@]+)[\/:].*(\.git)?$/\2/"
}

# If there are uncommitted changes, append an asterisk to the end of the prompt
function parse_git_dirty {
  git diff --no-ext-diff --quiet --exit-code &> /dev/null || echo "*"
}

# Parse out the branch 
function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/($(parse_git_remote_origin):\1$(parse_git_dirty))/"
}

# Now edit your command prompt setup, and add the $(parse_git_branch) function
PS1=$(parse_git_branch)$ 

Example of my .bashrc prompt

PS1='\[\033[01;32m\]\u@\h:\[\033[01;36m\]\w\[\033[00m\]\n$(parse_git_branch)\$ '

Looks like:

mbaxa@revan:~/git/mops/Proc
(mortech-stash.zillowgroup.net:master)$ 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment