Where exactly Git Bash for Windows' prompt is defined?

萝らか妹 提交于 2020-01-10 11:53:36

问题


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:

  • on office computer I have /c/path (branch) all green (this is, how I would like it to have),
  • 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. However, on both machines, this files is missing (executing notepad ~/.bashrc opens up empty Notepad).

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

Several answers (like this, this and this) has suggested me, that I should look for .bash_profile and .bash_prompt files. Unfortunately, these two also are missing on both my computers.

Exactly what file decides about that under 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?


回答1:


Git on Windows almost always uses a bash shell. So, it's not Git setting the prompt as much as Bash does.

There are two ways to set prompts in Bash. One is the PS1 command which is fairly flexible, but is limited to a particular set of escape character sequences. Unfortunately, Git information isn't one of those escape sequences (although I suspect it'll come someday). You can use the second way to set the prompt by setting the PROMPT_COMMAND environment variable. If this is set, the $PROMPT_COMMAND is executed and used as the prompt instead of the PS1 environment variable.

When you install the standard Git with BASH, you're Git prompt is defined under the /etc/profile file. 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.

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. (A third way I didn't mention previously).

The __git_ps1 is a shell function. 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. It's /etc/git-prompt.sh that defines the __git_ps1 function (Line #273 in my version) is defined. You'll notice that the __git_ps1 function pulls in several other functions defined in /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. Then, $PS1 uses the $(...) string as part of pulling in the __git_ps1 function into PS1.

You can define your own $HOME/.bash_profile to override the way the prompt is set to define your own prompt. And, when you do that, you can also use the __git_ps1 Bash function in your own prompt.

Or, you can simply decide not to touch anything, and just back away very slowly. After all, you may have actual work to do.




回答2:


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. Once I removed it, the issue got fixed. My Git Bash prompt is back to how it used to look.




回答3:


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). Look under your users where all users are located and search for .bashrc.

Look here : C:\Users\All Users or: C:\Users\Default User




回答4:


On my Windows10, "__git_ps1" is defined in: C:/Program Files/Git/etc/profile.d/git-prompt.sh



来源:https://stackoverflow.com/questions/22689548/where-exactly-git-bash-for-windows-prompt-is-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!