Skip to content

Updated technique for seeing git and directory status in bash prompt

Just working with a git directory and noticed that whatever trick I was using to get the current git branch visible from my $PS1 variable in bash … stopped working.  In discussing if a man page for git(1) was incorrect on #fedora-devel, tmz pointed out to me why it worked that way (short version: refer to the individual git-foo(1) man pages for actual examples, don’t rely upon the git(1) page for how the commands work on the command line.)  He also suggested an alternative that is inline with what we do now in git 1.6.  Here are the steps I followed.

  1. Rip out whatever logic you have in ~/.bashrc for displaying the git branch at your command prompt.
  2. Copy the script supplied by the git package to where you can make it an executable, such as ~/bin :
    • cp /etc/bash_completion.d/git ~/bin/git-completion.sh
  3. Make the script executable :
    • chmod 700 ~/bin/git-completion.sh
  4. Add these two lines to ~/.bashrc :
    • source ~/bin/git-completion.sh
    • export PS1=”[\u@\h \W”‘$(__git_ps1 ” (%s)”)'”]\$ “
  5. Reload your bashrc file :
    • . ~/bashrc

Enjoy having a peek at where you are in your git directory:

[kwade@calliope lookatgit (debug)]$ git branch
* debug
  help
  master
[kwade@calliope lookatgit (debug)]$