Git 提示和技巧:在命令提示符下显示分支不起作用并创建了 git branch 函数不起作用和许多其他的副作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2231214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:04:53  来源:igfitidea点击:

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

git

提问by Rachel

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

只是想增强我对 git 的了解并使用技巧和窍门,

I updated my .bashrc file as suggested in TipPS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ ' but to my surprise now my git branch command is not working and even if I remove it from .bashrc than also it is not working for me.

我更新了我的.bashrc文件中建议提示PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ ' ,但让我吃惊的,现在我的Git分支命令无法正常工作,即使我从.bashrc中删除它也比它不是对我来说有效。

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

此外,每次使用命令提示符时,我都会得到-bash: __git_ps1: command not found任何指导或建议吗?

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

更新:我现在想摆脱它,我怎样才能做到这一点?

采纳答案by Andrew Aylett

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

您是否启用了git bash-completion功能?您可能已经在系统上找到它们,只是未启用。

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).

要确定它们是否打开,请键入gitSpaceTab-- 如果您获得文件列表,则没有完成,如果获得命令列表,则可以(并且您的提示应该有效)。

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).

更新:从 .bashrc 中删除该行后,重新启动 shell 后,您遇到的问题应该会消失。仅仅重新获取文件将无济于事,因为它不会取消设置提示变量(无论如何,该变量在您覆盖它之前确实有一个值)。

回答by Steven Schlansker

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

通过谷歌搜索“__git_ps1”,我找到了第一个结果

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.

在 git 发行版中已经有一个函数可以做到这一点。如果您查看源代码,则有一个名为 contrib 的目录,其中包含一个 bash 完成文件,该文件为您提供 __git_ps1 函数。

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

所以听起来你需要去找到那个 contrib/ 文件并在你的 bash 配置文件中找到它!

回答by pepe gama

if you are using mac try this

如果您使用的是 mac 试试这个

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\] > '

回答by Ben Wheeler

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.

我认为最近版本的 git 将提示功能从 git-completion.bash 拆分为一个单独的 prompt.sh 文件......我不得不在我的 bash 配置文件中添加一个源代码行以包含该提示文件,然后我才得到摆脱 __git_ps1 not found 投诉。

回答by Gaurav

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

这对我有用只需转到您的 bash 配置文件 vi ~/bash_profile 并粘贴它

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

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