February 02, 2009

Posted by John

Older: HTTParty Divorces JSON

Newer: Shoulda Looked At It Sooner

Bedazzle Your Bash Prompt with Git Info

I have seen this around and this morning finally decided to try it out. Thus far I am finding it surprisingly helpful. If you put the following in your bash profile, it will show the current git branch in your terminal prompt.

bash profile addition

function parse_git_branch {
  ref=$(git-symbolic-ref HEAD 2> /dev/null) || return
  echo "("${ref#refs/heads/}")"

PS1="\w \$(parse_git_branch)\$ "

If you are not in a directory that is a git repository it will just provide a normal prompt but if you are in a directory with a git repo, you’ll get a prompt like the following, even when you switch branches:

~/dev/projects/httparty (master)$ gb
* master
~/dev/projects/httparty (master)$ git co integration
Switched to branch "integration"
~/dev/projects/httparty (integration)$

Pretty handy, eh? I actually bedazzled mine a bit more with color and the current time like this:

bedazzled bash profile addition

function parse_git_branch {
  ref=$(git-symbolic-ref HEAD 2> /dev/null) || return
  echo "("${ref#refs/heads/}")"


PS1="$RED\$(date +%H:%M) \w$YELLOW \$(parse_git_branch)$GREEN\$ "

Here is a screenshot of my prompt.

Terminal with git branch in prompt

Hawt. Light red blends into the black so as not to stand out. The yellow does standout, but that is because the current branch is more important to me than the directory I am in. The green color and black background is from the Homebrew theme in Terminal (requires Leopard). The reason I added the time is that I do not include the time in my menu bar. If you don’t like the colors I chose, you can pick from a list.

Try this out if you haven’t yet, I guarantee you will dig it. For those of you out there that use a different shell (zsh, etc.), feel free to post how to do the same in the comments.


  1. Richard Richard

    Feb 02, 2009

    You can also use the build-in Git routine which displays the status of git (i.e it’ll show you if you are part way through a merge, rebase etc), and does not get confused when the checkout is not at the head of a branch (it shows the commit SHA1).

    The contributed bash file also adds command line auto-completion for commands, tags and — parameters, which really speeds things up.

    Details on how to set this up here.

  2. lost arier lost arier

    Feb 03, 2009

    What about predefined things:?

    PS1='[\u@\h`__git_ps1` \W]\$ '

    [lost_arier@black-laptop (master) cucumber]$

    Simple? Is it not?

    Or play with another:
    _git_aliased_command __git_find_subcommand __git_ps1 __git_tags __git_aliases __git_has_doubledash __git_refs __gitcomp __git_all_commands __git_heads __git_refs2 __gitcomp_1 __git_complete_file __git_merge_strategies __git_refs_remotes __gitdir __git_complete_revlist __git_porcelain_commands __git_remotes

  3. lost arier: Nice tip, thanks!

  4. I’ve done something similar for my fish prompt:

      if git symbolic-ref HEAD >/dev/null ^/dev/null
        printf ' (git:%s' (git symbolic-ref HEAD ^/dev/null | awk -F/ '{print $3;}')
        if not test -z (gitout) >/dev/null ^/dev/null
          printf '+%d' (gitout | wc -l | awk '{print $1}')
        printf ')'

    gitout is a tiny function:

    function gitout
    git cherry -v (git-current-branch)

    This tells me that I’m in a git repo, which branch I’m in, and how many outgoing commits I have on that branch.

  5. Oh right, I forgot the aforementioned git-current-branch:

    function git-current-branch
    git symbolic-ref HEAD ^/dev/null | awk -F/ ‘{print $3;}’

  6. Nice article.

    I like the idea of putting the time in the prompt, but as shown, it will only show you the time it was when you opened the terminal (or, at least, that’s all it does for me). Any idea how to make it evaluate the current time, every time it shows the prompt?

  7. Nice idea, though for me it’s causing an annyoing delay after every command I issued on the shell.(say ‘ls’). With the aforementioned __git_ps1-command, it’s a bit faster, but still not as it was before modifying PS1. Anyway, here’s my snip (without colors)

    PS1="\w\$(__git_ps1)\$ "

    Anyone got an idea how to access the colors configured in gnome-terminal instead of the (guly) bash default ones?

  8. If you install git via MacPorts and activate the +bash_completion variant, it automatically provides a __git_ps1 function you can use in PS1 (I guess they’re the same predefined things lost arier listed above).

  9. bronson bronson

    Feb 10, 2009

    “git-symbolic-ref” should be “git symbolic-ref” to work on Git 1.6.

    Excellent, the bedazzled works on Linux too.

  10. I use a unicode skull and crossbones to show if there are uncommitted files in the current repo:


  11. @Geoffrey Nice! If only Apple would let you get that on your iPod…

Sorry, comments are closed for this article to ease the burden of pruning spam.


Authored by John Nunemaker (Noo-neh-maker), a programmer who has fallen deeply in love with Ruby. Learn More.


Release your software more often with fewer problems.
Flip your features.