31

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?

Community
  • 1
  • 1
trejder
  • 17,148
  • 27
  • 124
  • 216
  • You probably don't have the same versions of Git Bash on the two machines. What is displayed by the command 'echo $PS1' on each machine? – lrineau Mar 27 '14 at 14:10
  • A lot has changed since asked 8+ years ago. Main advice is to uninstall and reinstall [the latest version of Git Bash](https://gitforwindows.org#bash). Some settings are in the _file_ `/etc/profile`. Others are in the _folder_ `/etc/profile.d`. E.g., the files `bash_profile.sh` and `git-prompt.sh` are in `/etc/profile.d`. Seen from Windows (File Explorer), this is in `C:\\Git\etc\profile.d`, where `` might be `Program Files` or `Program Files (x86)`. If `~/.bashrc` exists, it should be in `%UserProfile%`. So should `.bash_profile`. – Henke Aug 14 '22 at 18:34

4 Answers4

30

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.

David W.
  • 105,218
  • 39
  • 216
  • 337
  • Thanks for your enlightening and detailed answer. I have to verify this with my home computer, but as I wrote in my question `c:\Program Files\Git\etc\` folders on both computers are identical as in files count and contents (checked by Total Commander's sync tool). And even so, prompt differs on both machines. Even, since you didn't solved my problem (at least for now), it may turn out to be too localized (occurring only here). But you did answered a detailed answer to given question, so I'm accepting your answer, even tough it "_didn't work out for me_" (in terms of SE FAQ). Thanks! :] – trejder Mar 28 '14 at 08:41
  • 1
    Is it possible that your `$HOME` directory on one or the other machines have a `.profile`, `.bash_profile`, or `.bashrc` file, and that's where the prompt is getting set? Echo both `$PS1` and `$PROMPT_COMMAND` on both systems. Also check the _file modes_ on both systems. If a file isn't marked as executable, it's not sourced in when logging in. – David W. Mar 28 '14 at 14:25
  • Uninstall, restart, Re Install (1st step or 2nd step, click customize button) on a non c:\program files... folder like c:\apps\git – tgkprog Jun 30 '16 at 04:47
  • 3
    For git 2.21.0 it is in /etc/profile.d/git-prompt.sh – Thorbjørn Ravn Andersen Apr 30 '19 at 11:54
6

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

Henke
  • 4,445
  • 3
  • 31
  • 44
Emily
  • 306
  • 4
  • 8
  • 1
    For my Win10 box it was: "C:\Program Files\Git\mingw64\share\git\completion\git-prompt.sh" – Smola Dec 26 '18 at 09:49
3

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.

Codeip
  • 31
  • 2
2

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

  • Nope. Before posting this question, I've searched my entire system drive (`c:`) for any trace of `.bashrc`, `.bash_profile` and `.bash_prompt` files. None of them exists in any of `c:` drive subfolders (including `Users` folder for local user settings and `Program Files`, where Git is installed). Thanks for your effort, but this is certainly wrong direction. – trejder Mar 28 '14 at 08:35