joan
joan

Reputation: 2531

How can I modify my UNIX prompt to show the name of the current branch?

I want to modify my shell prompt so that it show the name of the current branch. Is there any easy way to do it?

(For information, I'm using Bash on Mac OS X.)

Upvotes: 1

Views: 250

Answers (2)

jub0bs
jub0bs

Reputation: 66224

No need for a third-party tool: Git already provides a shell script for adding the current branch name (if any) to your shell prompt; it's compatible with both bash and zsh.

Simply download that script, and then follow the installation instructions:

  1. Copy this file to somewhere (e.g. ~/.git-prompt.sh).
  2. Add the following line to your .bashrc/.zshrc:

    source ~/.git-prompt.sh
    
  3. Change your PS1 to call __git_ps1 as command-substitution:

    • Bash: PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
    • ZSH: setopt PROMPT_SUBST ; PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '

    the optional argument will be used as format string.

You might prefer a more lightweight prompt than the one given; for example, I've set mine to

export PS1='\W$(__git_ps1 "(%s)")\$ '

instead.

Finally, relaunch Terminal (or source ~/.bash_profile, since you're using bash) and you're good to go:

enter image description here

Upvotes: 2

Thomas Ayoub
Thomas Ayoub

Reputation: 29431

You can use BashIt which is a bundle of a loooot of useful things (including git branching display) as shown below: enter image description here

Upvotes: 1

Related Questions