Skip to content

Instantly share code, notes, and snippets.

@justintv
Created August 17, 2009 00:53
Show Gist options
  • Save justintv/168835 to your computer and use it in GitHub Desktop.
Save justintv/168835 to your computer and use it in GitHub Desktop.
Display git branch in bash prompt
# If you work with git, you've probably had that nagging sensation of not knowing what branch you are on. Worry no longer!
export PS1="\\w:\$(git branch 2>/dev/null | grep '^*' | colrm 1 2)\$ "
# This will change your prompt to display not only your working directory but also your current git branch, if you have one. Pretty nifty!
# ~/code/web:beta_directory$ git checkout master
# Switched to branch "master"
# ~/code/web:master$ git checkout beta_directory
# Switched to branch "beta_directory"
# ~/code/web:beta_directory$
@JkktBkkt
Copy link

Any way to make the path part of the prompt git-aware?
I'd want it to behave similarly to how it is homedir aware, with highlighting via different colors git's rootdir (not submodule's rootdir, mind you) and the rest of the path
Sort of like this: ~/path/to/GITROOT (BRANCH) path/in/tree
if not even GITROOT (BRANCH) path/in/tree ignoring path to the root

@JkktBkkt
Copy link

edit: ended up piecing a few things together and overriding the prompt function with an external script.
Careful, even if a lot of people above are talking ZSH, I'm doing bash things here as the original author of the gist did too

Using this stackoverflow answer and this one as basis and sourcing /usr/share/git/git-prompt.sh (path will be different on other distros) above this function
The path section ended up being:

   # time to manipulate path, let's check if path is in git
   local git_top=$(git rev-parse --show-toplevel 2>/dev/null)
   if [ "$git_top" != "" ]; then
        # we're in git
        # substitute homedir
        git_relative_top=${git_top/$HOME/\~}
        reponame=${git_top##*/}
        # green non-bold path to repo
        PS1+="\[$txtreset\]\[$txtgreen\]${git_relative_top%$reponame}"
        # repo itself is bold
        PS1+="\[$txtbold\]$reponame\[$txtreset\]"
        # now the branch info in blue
        # apply the git ps1 sourced in bash.rc earlier 
        # args are "what is before the git-part" "what is after it" 'what to ask from git'
        __git_ps1 "$PS1" "\[$txtreset\]\[$txtblue\]\[$txtbold\]${PWD#$git_top}" '(%s)'
   else
        # Not in git, let's use default green home-aware path
        PS1+="\[$txtbold\]\[$txtgreen\]\w"
   fi;

The whole script I've ended up writing:

set_prompt()
{
   local last_cmd_return_code=$?
   local txtreset='$(tput sgr0)'
   local txtbold='$(tput bold)'
   local txtblack='$(tput setaf 0)'
   local txtred='$(tput setaf 1)'
   local txtgreen='$(tput setaf 2)'
   local txtyellow='$(tput setaf 3)'
   local txtblue='$(tput setaf 4)'
   local txtpurple='$(tput setaf 5)'
   local txtcyan='$(tput setaf 6)'
   local txtwhite='$(tput setaf 7)'
   # escaping these so that it doesn't break multi-line prompts
   # unicode "✗"
   local fancyx='\342\[\234\227\]'
   # unicode "✓"
   local checkmark='\342\[\234\223\]'
   # clearing ps1
   PS1=""
   # Part 1: a red "✗" or a green "✓" and the error number
   if [[ $last_cmd_return_code == 0 ]]; then
      PS1+="\[$txtgreen\]$checkmark\[$txtreset\] "
   else
      PS1+="\[$txtred\]$fancyx \[$txtreset\]$last_cmd_return_code "
   fi
   # Part 2: Add user
   # default to bold
   PS1+="\[$txtbold\]"
   # red when we're root / id 0
   if [[ $EUID == 0 ]]; then
       PS1+="\[$txtred\]"
   elif [[ $UID == 1000 ]]; then
   # main user of the system gets purple
       PS1+="\[$txtpurple\]"
   else
   # the rest are default and non-bold
       PS1+="\[$txtreset\]"
   fi
   # Part 3: user@host
   PS1+="\u\[$txtreset\]@\[$txtbold\]\[$txtcyan\]\h\[$txtreset\]:"
   # Part 4: time to manipulate path, let's check if path is in git
   local git_top=$(git rev-parse --show-toplevel 2>/dev/null)
   if [ "$git_top" != "" ]; then
        # we're in git
        # substitute homedir
        git_relative_top=${git_top/$HOME/\~}
        reponame=${git_top##*/}
        # green non-bold path to repo
        PS1+="\[$txtreset\]\[$txtgreen\]${git_relative_top%$reponame}"
        # repo itself is bold
        PS1+="\[$txtbold\]$reponame\[$txtreset\]"
        # now the branch info in blue
        # apply the git ps1 sourced in bash.rc earlier
        # args are "what is before the git-part" "what is after it" 'what to ask from git'
        __git_ps1 "$PS1" "\[$txtreset\]\[$txtblue\]\[$txtbold\]${PWD#$git_top}" '(%s)'
   else
        # Not in git, let's use default green home-aware path
        PS1+="\[$txtbold\]\[$txtgreen\]\w"
   fi;
   # Prompt, $ for user, # for root
   if [[ $EUID == 0 ]]; then
       PS1+="\[$txtreset\]\[$txtred\]"
   else
       PS1+="\[$txtreset\]\[$txtwhite\]"
   fi
   PS1+="\\$\[$txtreset\] "
#   echo "$PS1"
}
PROMPT_COMMAND='set_prompt'

This ends up looking like so:
image
checkmark or cross and status for last command,
then user@hostname:~/path/to/project (branch)/path/in/repo

@joejoseph00
Copy link

https://gist.github.com/justintv/168835?permalink_comment_id=3718502#gistcomment-3718502 I've been using this approach on all my servers, looks great, I'll provide an update at some point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment