开发者

Git Tips and Tricks : Display Branch on Command Prompt not working and created side effects of git branch function not working and many others

Just trying to enchance my knowledge of git and use to tips and tricks,

I updated my .bashrc file as suggested in Tip PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ ' but to my surprise no开发者_JS百科w my git branch command is not working and even if I remove it from .bashrc than also it is not working for me.

Also every time, I use my command prompt I get -bash: __git_ps1: command not found any guidance or suggestions ?

Update : I want to get rid of it now, how can I achieve this ?


Have you enabled the git bash-completion functions? You may find them on your system already, just not enabled.

To work out whether they're on or not, type gitSpaceTab -- If you get a list of files, you don't have completion, if you get a list of commands, you do (and your prompt should work).

Update: Once you've removed the line from your .bashrc, the issues you've been experiencing should go away once you've restarted your shell. Merely re-sourcing the file won't help, as it doesn't un-set the prompt variable (and in any case, that variable did have a value before you overrode it).


By googling for "__git_ps1", I found the first result which says:

There is already a function to do this in the git distribution. If you check out the source there is a directory called contrib which has a bash completion file that gives you a __git_ps1 function.

So sounds like you need to go find that contrib/ file and source it in your bash profile!


if you are using mac try this

sudo port selfupdate
sudo port install git-core +bash_completion
then 
cd /private/etc/
sudo vi bashrc
and add the next
source /opt/local/share/doc/git-core/contrib/completion/git-completion.bash
if [ -f /opt/local/etc/bash_completion ]; then
   . /opt/local/etc/bash_completion
fi

export PS1='\w $(__git_ps1 "\[\e[31m\]:%s")\[\e[0m\] > '


i think recent versions of git split off the prompt functions from git-completion.bash into a separate prompt.sh file... i had to add a source line to my bash profile to include that prompt file, and only then did i get rid of the __git_ps1 not found complaint.


This worked for me Just go to your bash profile vi ~/bash_profile and paste this

parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

export PS1="\u@\h \[\033[32m\]\w - \$(parse_git_branch)\[\033[00m\] $ "
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜