简体   繁体   English

究竟是哪里定义了Git Bash for Windows'提示符?

[英]Where exactly Git Bash for Windows' prompt is defined?

Both my office and home computers have Git Bash for Windows from the very same source (Git for Windows, that came with TortoiseGit), but Git Bash's prompt differs on both machines: 我的办公室和家用电脑都有来自同一来源的Git Bash for Windows(Git for Windows,TortoiseGit附带),但Git Bash的提示在两台机器上都有所不同:

  • on office computer I have /c/path (branch) all green (this is, how I would like it to have), 在办公室电脑上我有/c/path (branch)全绿色(这是我希望它有的方式),
  • on home computer I have only path (no branch) and all white. 在家用电脑上我只有路径(没有分支)和全白。

Up until now I was told, that this is PS1 variable, kept in ~/.bashrc file. 到目前为止我被告知,这是PS1变量,保存在~/.bashrc文件中。 However, on both machines, this files is missing (executing notepad ~/.bashrc opens up empty Notepad). 但是,在这两台机器上,这个文件都丢失了(执行notepad ~/.bashrc打开空记事本)。

I'm lost here. 我迷失在这里。 If ~/.bashrc doesn't exists, then from where Git Bash "knows", that it should display current branch, in a green prompt? 如果~/.bashrc不存在,那么从Git Bash“知道”的地方,它应该在绿色提示中显示当前分支? And why the same doesn't happen on second machine? 为什么在第二台机器上不会发生同样的情况?

EDIT : I also tried to compare c:\\Program Files\\Git\\etc\\ folder contents for both machines and contents of Git Bash.vbs file. 编辑我还尝试比较Git Bash.vbs文件的机器和内容的c:\\Program Files\\Git\\etc\\文件夹内容。 There identical on both computers so I even more have no idea, how it can be, that Git Bash's prompt differs on both computers (and how to fix this). 在两台计算机上都是相同的,所以我更不知道,Git Bash的提示在两台计算机上都有所不同(以及如何解决这个问题)。

Several answers (like this , this and this ) has suggested me, that I should look for .bash_profile and .bash_prompt files. 几个答案(像这样这个这个 )建议我,我应该寻找.bash_profile.bash_prompt文件。 Unfortunately, these two also are missing on both my computers. 不幸的是,我的两台计算机上都缺少这两个。

Exactly what file decides about that under Windows 7? 究竟是什么文件决定在Windows 7下? Where else should I look for, and what PS1 variable's value should be, to have current branch shown in green prompt on both machines? 我应该在哪里寻找,以及PS1变量的值应该是什么,以便在两台机器上以绿色提示显示当前分支?

Git on Windows almost always uses a bash shell. Windows上的Git几乎总是使用bash shell。 So, it's not Git setting the prompt as much as Bash does. 所以,Git并不像Bash那样设置提示

There are two ways to set prompts in Bash. 有两种方法可以在Bash中设置提示。 One is the PS1 command which is fairly flexible, but is limited to a particular set of escape character sequences. 一个是PS1命令,它相当灵活,但仅限于一组特定的转义字符序列。 Unfortunately, Git information isn't one of those escape sequences (although I suspect it'll come someday). 不幸的是,Git信息不是那些逃逸序列之一(虽然我怀疑它有一天会到来)。 You can use the second way to set the prompt by setting the PROMPT_COMMAND environment variable. 您可以使用第二种方法通过设置PROMPT_COMMAND环境变量来设置提示。 If this is set, the $PROMPT_COMMAND is executed and used as the prompt instead of the PS1 environment variable. 如果设置了$PROMPT_COMMAND则执行$PROMPT_COMMAND并将其用作提示而不是PS1环境变量。

When you install the standard Git with BASH, you're Git prompt is defined under the /etc/profile file. 当您使用BASH安装标准Git时,您的Git提示符在/etc/profile文件下定义。 By the way, etc is a directory under where you've installed Git which is usually under %PROGRAMFILES% unless you changed it when you installed Git. 顺便说一句, etc是你安装Git的目录,它通常在%PROGRAMFILES%之下,除非你在安装Git时更改了它。

Under the /etc/profile script in line #156 in my version, you see the PS1 command being set and using $(__git_ps1) in $PS1 as a means of executing an external command in the prompt. 在我的版本中第156行的/etc/profile脚本下,您会看到正在设置PS1命令并在$PS1使用$(__git_ps1)作为在提示中执行外部命令的方法。 (A third way I didn't mention previously). (我之前没有提及的第三种方式)。

The __git_ps1 is a shell function. __git_ps1是一个shell函数。 You'll also notice a bit above (line #154 in my version) that /etc/git-completion.bash is being sourced in as well as /etc/git-prompt.sh . 您还会注意到/etc/git-completion.bash以及/etc/git-prompt.sh (我的版本中的第154行)。 It's /etc/git-prompt.sh that defines the __git_ps1 function (Line #273 in my version) is defined. 这是/etc/git-prompt.sh定义__git_ps1 (在我的版本号线#273)函数的定义。 You'll notice that the __git_ps1 function pulls in several other functions defined in /etc/git-prompt.sh . 你会发现, __git_ps1功能中定义的几个其他功能拉/etc/git-prompt.sh

So, in a very Rube Goldberg manner, the Git prompt is being defined in /etc/profile via defining $PS1 which pulls in /etc/git-prompt.sh which defines a __git_ps1 function that pulls in the __git_ps1_show_upstream function and the __git_ps1_colorize_gitstring function. 因此,以非常Rube Goldberg的方式,通过定义$PS1来定义/etc/profile的Git提示符,该$PS1 /etc/git-prompt.sh ,它定义__git_ps1函数,该函数__git_ps1_show_upstream函数和__git_ps1_colorize_gitstring函数。 Then, $PS1 uses the $(...) string as part of pulling in the __git_ps1 function into PS1 . 然后, $PS1使用$(...)字符串作为将__git_ps1函数拉入PS1

You can define your own $HOME/.bash_profile to override the way the prompt is set to define your own prompt. 您可以定义自己的$HOME/.bash_profile以覆盖设置提示以定义自己的提示的方式。 And, when you do that, you can also use the __git_ps1 Bash function in your own prompt. 而且,当你这样做时,你也可以在你自己的提示中使用__git_ps1 Bash函数。

Or, you can simply decide not to touch anything, and just back away very slowly. 或者,你可以简单地决定不接触任何东西,然后慢慢地退回去。 After all, you may have actual work to do. 毕竟,你可能有实际的工作要做。

I faced similar issue and realized that accidently I had added ${HOME} variable under environment/system variables(This PC) in my windows 10 64 bit pointing to my unixhome path. 我遇到了类似的问题,并意识到我在我的Windows 10 64位指向我的unixhome路径的环境/系统变量(这台PC)下添加了$ {HOME}变量。 Once I removed it, the issue got fixed. 一旦我删除它,问题得到修复。 My Git Bash prompt is back to how it used to look. 我的Git Bash提示回到过去的样子。

I would make a comment if I would have enough reputation, 如果我有足够的声誉,我会发表评论,

but my guess is that the bashrc is not in your homefolder: ~/ but in the all users or general user folder(I dont know how it is named exactly). 但我的猜测是bashrc不在你的homefolder中:〜/但在所有用户或一般用户文件夹中(我不知道它是如何命名的)。 Look under your users where all users are located and search for .bashrc. 查看所有用户所在的用户,并搜索.bashrc。

Look here : C:\\Users\\All Users or: C:\\Users\\Default User 在这里查看:C:\\ Users \\ All Users或:C:\\ Users \\ Default User

在我的Windows10上,“__ git_ps1”定义在:C:/ Program Files / Git / etc / profile.d / git-prompt.sh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM