26

I'm using Git on Windows, installed through GitExtensions with MSysGit (latest) having selected "do not modify my Windows prompt" during installation.

Now, I would like to be able to modify the default prompt (which by default shows just the branch name to also show me how much time, and how many local commits since I last pushed to origin (or specifically origin/master, whichever is easier).

So say instead of: me@myPC /c/myRepo (master)

I would see something along the lines of: me@myPC /c/myRepo (master) 5 | 10:20

meaning I have last pushed 10h 20min ago and I have made 5 local commits since.

Before you mention it, I am aware there are ways of doing it with PowerShell, but I don't want to use it. I want my standard git bash we all know and love.

I found a few solutions to that, with modifying PS1 variable in .bashrc file, but (excuse my poor Unix konwledge) they seem to be not working, (for example accepted answer to this question).

So there you have it. Is this possible?

Community
  • 1
  • 1
kko
  • 491
  • 1
  • 4
  • 8

4 Answers4

12

The default git-completion script supports part of what you are after. If you set GIT_PS1_SHOWUPSTREAM="verbose git" in the /etc/profile file then it will add the number of commits ahead of your upstream branch into the prompt. You may need to set the prompt as below to include a (%s) in the git specific part:

export PS1='\[\033]0;$MSYSTEM:\w\007
\033[32m\]\u@\h \[\033[33m\w$(__git_ps1 " (%s)")\033[0m\]
\$ '

For the time part - thats new to me. But the git-bash should handle any unix version you may have found. Just edit /etc/profile as administrator (its actually %PROGRAMFILES%\Git\etc\profile or create a ~/.profile file containing the following:

GIT_PS1_SHOWDIRTYSTATE=1
GIT_PS1_SHOWUPSTREAM='verbose git'
export GIT_PS1_SHOWDIRTYSTATE GIT_PS1_SHOWUPSTREAM

with these environment variables set, the default msysGit prompt looks like this if you have a dirty tree with 1 commit ahead of origin:

pat@FROG /c/src/msysgit (devel * u+1)
$ git status --short --branch
## devel...origin/devel [ahead 1]
 M doc/git/html
 M etc/inputrc
patthoyts
  • 32,320
  • 3
  • 62
  • 93
  • cheers. So I just added those two pieces at the end of my profile file, I started a new prompt window but nothing really changed. Any ideas as to why that might be? I doublechecked I don't run some other installation of `sh.exe` (that lives in some other folder and therefore is not affected by any of those changes) but it is the one from `c:\Program Files (x86)\Git\bin\` – kko Apr 13 '12 at 00:56
  • Not much will change unless you have commits ahead of your remote. The " (%s)" addition to the default PS1 permits inclusion of additions determined by the environment variables mentioned in the start of the git-completion script file. To test, insert 'test' before the \$ on the last line of the PS1 prompt. If your text fails to appear, you are overriding someplace else. There is a list of locations at the end of the /etc/profile script that are read in for overrides. – patthoyts Apr 13 '12 at 08:17
6

step 1. Copy these two files to your home folder, you may find them under %PROGRAMFILES%\Git\etc\;

git-completion.bash
git-prompt.sh

step 2. Config your PS1 in your bash profile like .bashrc, for example:

. git-completion.bash
. git-prompt.sh
GIT_PS1_SHOWDIRTYSTATE=true
PS1='\w\[\033[01;32m\]$(__git_ps1)\[\033[00m\]\$ '
aufula
  • 140
  • 2
  • 6
  • When you say "in your bash profile" I assume you mean a file named profile with those four lines above. I've done that then reopened the Git Bash window and there is no change to the default prompt. Typing `echo $PS1` shows the default PS1 value. – Rich Shealer May 10 '13 at 03:07
  • @RichShealer Because there are diffent ways to set bash profile, for me, I make alias to _mybash under my git repository folder. Maybe you like to config everything in .bashrc – aufula May 10 '13 at 07:22
  • Thanks for the edit. It is confusing to a first time user because of the variation in environments (OSs) supported and the tendancy to use generic descriptions such as "profile". – Rich Shealer May 10 '13 at 09:47
  • With the new installation of git, these files are now under `%PROGRAMFILES%\Git\etc\profile.d\` – jaques-sam May 13 '19 at 11:16
4

The __git_ps1 function is defined in /git/config/completion/git-completion.bash from within mingw bash. You should be able to copy this function to your own .bashrc and edit it as you choose.

FWIW I chose to skip __git_ps1 and to my own thing with color. Here's the prompt code from my .bashrc:

DEFAULT_COLOR='\[\e[0m\]'
BLACK='\[\e[0;30m\]'
LTBLUE='\[\e[1;34m\]'
LTRED='\[\e[1;31m\]'

USER_COLOR=$LTBLUE
GIT_CHANGE_COLOR=$LTRED
GIT_NO_CHANGE_COLOR="$BLACK"

gitPrompt() {
  local gitPrompt=$(__git_ps1)
  local gitColor="$GIT_NO_CHANGE_COLOR"
  if [ -n "$gitPrompt" ]; then
    branch=$(git symbolic-ref HEAD 2>/dev/null)
    if [ "$gitPrompt" != " (${branch##refs/heads/})" ]; then
      gitColor="$GIT_CHANGE_COLOR"
    fi
  fi
  echo "$USER_COLOR\u@\h:\W$gitColor$gitPrompt$DEFAULT_COLOR$ "
}

PROMPT_COMMAND='PS1="$(gitPrompt)"'

The expression in PROMPT_COMMAND gets evaluated each time the prompt is printed.

To do what you want, you would probably have to parse the reflog with something like awk or perl and append that after the branch name. I usually rely on gitk to visualize the information you're looking for.

rlduffy
  • 608
  • 4
  • 8
0

If you run GitPortable for Windows you may want to do this trick taken from this site

Write $ nano ~/.bashrc in gitportable console. After you have done editing write $ source ~/.bashrc to reload settings. My installation saves a file in C:\apps\GitPortable\Data\home folder.

# Git-friendly prompt showing dirty state
# http://stackoverflow.com/questions/10133173/alter-git-prompt-on-windows

shopt -s promptvars
export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_SHOWUPSTREAM='verbose git'
export PS1='\[\033[1;36m\]\[\033[0m\]\[\033[1;34m\]\w\[\033[0m\] 
\[\033[1;32m\]$(__git_ps1)\[\033[0m\]\$ '

# Enable color support of ls
alias ls='ls --color=auto -alhX'
Whome
  • 10,181
  • 6
  • 53
  • 65