Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alter Git prompt on Windows

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?

like image 802
kko Avatar asked Sep 10 '25 20:09

kko


2 Answers

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
like image 158
patthoyts Avatar answered Sep 12 '25 10:09

patthoyts


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\]\$ '
like image 36
aufula Avatar answered Sep 12 '25 10:09

aufula