Reputation: 2531
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
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:
- Copy this file to somewhere (e.g.
~/.git-prompt.sh
).Add the following line to your
.bashrc/.zshrc
:source ~/.git-prompt.sh
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:
Upvotes: 2
Reputation: 29431
You can use BashIt which is a bundle of a loooot of useful things (including git branching display) as shown below:
Upvotes: 1