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?
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?
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) >
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
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
# 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
add source ~/.bash_profile in .bashrc.
Had the same problem and it just worked for me.
This was tested on debian/ubuntu.
bash-completion package~/.bashrc and are not commented out.if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
. /etc/bash_completion
fi
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.
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