Skip to main content

Show Git / Mercurial / SVN / Bazaar branch in the Bash prompt

Clipart image of a blank command prompt
This is my solution for modifying the Bash prompt (PS1) to show the branch name. It works for any working directory in a Git, Mercurial, Subversion or Bazaar branch. [2 minutes]

The function works by looking for a .git, .hg, .svn or .bzr in any of the ancestor directories, and if found uses appropriate commands like __git_ps1, hg branch, svn info to deduce the branch name to prepend to the PS1 prompt.  For Bazaar it avoids the slow "bzr nick" command by looking in branch.conf directly. Here is the embedded gist:



Here is a link to the show branch name in Bash prompt gist, for when the embedding is not displaying.

Super handy if you have lots of checkouts all over the place!

P.S. if you are into working more efficiently, try this summary of Time Management for System Administrators (applies just as much to DevOps and SRE).

Did you find this useful, have any improvements, or find any bugs? Comment below!

Comments

  1. Awesome. If you want to see a tooled out (ZSH) prompt for mercurial, check out the screen shots here http://hgtip.com/tips/advanced/2010-01-15-styling-mercurials-cli/

    ReplyDelete
  2. Anonymous15 September

    You can also take a look at __git_ps1 "(%s)"

    Awesome set of functions

    ReplyDelete

Post a Comment

Popular posts from this blog

How to grow a large crystal of copper (II) sulphate in 5 days

Presenting a faster way to grow large copper sulphate crystals! The pictured 4cm crystal took me 5 days by cooling, instead of the 3-6 weeks it would take by evaporation. [4 minute read]

How to write Bad Python

A controversial post about bad code that I observed on an unnamed Python project, in which I describe outdated idioms, Java-style code, and bad programming practices. [3 minutes]

Declutter your home with the Outbox Method

Here are some easy ways to reduce the clutter at home by means of an "Outbox". It's a box for things that you think you might not need, that you then periodically empty. [4 minutes]