24

I updated my .bashrc file as follows:

PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$'

It works just find and I can see my branch name in the prompt. However,when I run "screen" , I get

"-bash: __git_ps1: command not found"

What can be the reason for this?

N.N.
  • 8,336
  • 12
  • 54
  • 94
Ohad
  • 1,450
  • 4
  • 18
  • 27
  • Have a look at this thread: http://stackoverflow.com/questions/2231214/git-tips-and-tricks-display-branch-on-command-prompt-not-working-and-created-s – ChristofferH Mar 15 '12 at 09:59

8 Answers8

29

This blog post explains that you have to add the line source /etc/bash_completion.d/git before you can use __git_ps1.

Here is the full example:

source /etc/bash_completion.d/git  
export PS1='\w$(__git_ps1 "(%s)") > '

This also enables auto completion for branches.

Using that formatting, your prompt will resemble (without colouring):

~/my-repo(master) > 
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
SeriousM
  • 3,374
  • 28
  • 33
  • how do you do that but still retain the normal prompt information? (ie. just append the respective name) – srcspider Mar 22 '13 at 11:49
  • 7
    In Ubuntu 13.04, it seems that you have to source `/etc/bash_completion.d/git-prompt` (which in turn sources `/usr/lib/git-core/git-sh-prompt`) instead of `/etc/bash_completion.d/git` . (You might have to look around for the correct completion file to source...) – michael Apr 26 '13 at 08:39
  • 1
    __git_ps1 was moved out of git-completion. See http://stackoverflow.com/a/12871094/551045 for more information – RedX Oct 30 '13 at 12:30
  • 1
    @michael_n, that also works for Ubuntu 14.04! Thanks! – Marcello DeSales Aug 04 '14 at 21:41
24

I find it cleaner to modify the existing prompt instead of defining a new one. The following snippet adds the git branch name to the existing prompt (which is $PS1). You can add the following snippet to the ~/.bashrc file:

source /etc/bash_completion.d/git (for Ubuntu 12.04 or less)
source /etc/bash_completion.d/git-prompt (for Ubuntu 13.04 and higher)
PS1=$PS1'$(__git_ps1 "(%s) ")'

If you want to have the branch name in color you can do that too: For example, the color green is defined as [\e[0;32m]. We add this to the inside string of the git_ps1 function and reset the color using \e[0m afterwards. The escaped brackets are required to indicate that "special" characters are inserted.

PS1=$PS1'$(__git_ps1 "\[\e[0;32m\](%s) \[\e[0m\]")'

Many other color definitions can be found here

msteiger
  • 2,024
  • 16
  • 22
8

The problem is that bash needs to be run as a login shell for this function to be available in the default cygwin setup. If you run bash in a cygwin bash you will have the same problem. To set screen to run bash in login mode, add this line to your ~/.screenrc file:

shell -bash
Chronial
  • 66,706
  • 14
  • 93
  • 99
7
# Add following line to /.bashrc to show Git branch name in ssh prompt
PS1='\[\033[0;31m\]\w\[\033[0;33m\]$(__git_ps1)\[\e[0m\]$ '

\[\033[0;31m\] is red

\[\033[0;33m\] is yellow

\[\e[0m\] is normal

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
Mru
  • 241
  • 3
  • 4
3

add source ~/.bash_profile in .bashrc.

Had the same problem and it just worked for me.

darksky
  • 20,411
  • 61
  • 165
  • 254
0

This was tested on debian/ubuntu.


  1. install bash-completion package
  2. make sure the following lines exist in your ~/.bashrc and are not commented out.

if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
     . /etc/bash_completion
fi
sjas
  • 18,644
  • 14
  • 87
  • 92
0

If you dont have __git_ps1 you could use

git branch --contains HEAD 2>/dev/null

It displays the same like __git_ps1.

And if you create an alias like this:

alias __git_ps1='git branch --contains HEAD 2>/dev/null'

e.g. your prombt you get with this command:

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

or with

PS1='[\u@\h \W\[\033[36m\](`__git_ps1`)\[\033[0m\]]\$'

if you like colors

Your scripts which use __git_ps1 and you promt will work perfect.

Radon8472
  • 4,285
  • 1
  • 33
  • 41
0
root:~/project#  -> root:~/project(dev)# 

add the following code to the end of your ~/.bashrc

force_color_prompt=yes
color_prompt=yes
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '
fi
unset color_prompt force_color_prompt
Ali Alp
  • 691
  • 7
  • 10