How to configure Git bash prompt by adding datetime?
Asked Answered
R

3

7

My msysgit Git bash command-line prompt looks like this right now:

GitUserName@WorkStationName WorkSpacePath (BranchName)

I would like to have a timestamp in front of that line, like HH:mm (hours:minutes).

Does anyone know how I can easily do this?

Rahel answered 21/10, 2012 at 20:30 Comment(0)
S
3

If by date you mean the current time/date, then this example can help:

 PS1="\n\[\033[35m\]\$(/bin/date)\n\[\033[32m\]\w\n\[\033[1;31m\]\u@\h: \[\033[1;34m\]\$(/usr/bin/tty | /bin/sed -e 's:/dev/::'): \[\033[1;36m\]\$(/bin/ls -1 | /usr/bin/wc -l | /bin/sed 's: ::g') files \[\033[1;33m\]\$(/bin/ls -lah | /bin/grep -m 1 total | /bin/sed 's/total //')b\[\033[0m\] -> \[\033[0m\]"

git prompt with date

It uses $(/bin/date) and is a multi-line prompt containing date/time, full path, user and host, active terminal, even file count and space usage.

It illustrates how you could integrate the date in your own git prompt.


The OP deblendewim comments:

I was also wondering how to change it without running it immediatly in the prompt.
Changed profile-file from:

if test -z "$WINELOADERNOEXEC" then PS1='\[\033]0;$MSYSTEM:\w\007 \033[32m\]\u@\h \[\033[33m\w$(__git_ps1)\033[0m\] $ ' else PS1='\[\033]0;$MSYSTEM:\w\007 \033[32m\]\u@\h \[\033[33m\w\033[0m\] $ ' 

into

if test -z "$WINELOADERNOEXEC" then PS1='\[\033]0;$MSYSTEM:\w\007 \[\033[36m\]\t \[\033[32m\]\u@\h \[\033[33m\w$(__git_ps1)\033[0m\] $ ' else PS1='\[\033]0;$MSYSTEM:\w\007 \[\033[36m\]\t \[\033[32m\]\u@\h \[\033[33m\w\033[0m\] $ ' 
Stannary answered 22/10, 2012 at 6:8 Comment(2)
Thanks VonC. The link definitly helped me out! I was also wondering how to change it without running it immediatly in the prompt. Changed profile-file from: if test -z "$WINELOADERNOEXEC" then PS1='\[\033]0;$MSYSTEM:\w\007 \033[32m\]\u@\h \[\033[33m\w$(__git_ps1)\033[0m\] $ ' else PS1='\[\033]0;$MSYSTEM:\w\007 \033[32m\]\u@\h \[\033[33m\w\033[0m\] $ ' into if test -z "$WINELOADERNOEXEC" then PS1='\[\033]0;$MSYSTEM:\w\007 \[\033[36m\]\t \[\033[32m\]\u@\h \[\033[33m\w$(__git_ps1)\033[0m\] $ ' else PS1='\[\033]0;$MSYSTEM:\w\007 \[\033[36m\]\t \[\033[32m\]\u@\h \[\033[33m\w\033[0m\] $ 'Rahel
the code I'm talking about in the above comment can be found here: C:\Program Files (x86)\Git\etc\profile (the file is called profile) somewhere around line 155Rahel
A
9

I've found that using the file \Git\etc\profile.d\git-prompt.sh you can add a line that looks like this to add datetime to your prompt

PS1="$PS1"'\[\033[35m\]\D{%F %T} ' (as seen here for formatting details)

My prompt currently looks like this

2015-11-08 23:48:08 Sean@pc-01 /c/dev/projects

PS1='\[\033]0;$MSYSTEM:${PWD//[^[:ascii:]]/?}\007\]' # set window title
PS1="$PS1"'\n'                 # new line
PS1="$PS1"'\[\033[35m\]\D{%F %T} '
PS1="$PS1"'\[\033[32m\]'       # change to green
PS1="$PS1"'\u@\h '             # user@host<space>
PS1="$PS1"'\[\033[35m\]'       # change to purple
#PS1="$PS1"'$MSYSTEM '          # show MSYSTEM
PS1="$PS1"'\[\033[33m\]'       # change to brownish yellow
PS1="$PS1"'\w'                 # current working directory
if test -z "$WINELOADERNOEXEC"
then
    GIT_EXEC_PATH="$(git --exec-path 2>/dev/null)"
    COMPLETION_PATH="${GIT_EXEC_PATH%/libexec/git-core}"
    COMPLETION_PATH="${COMPLETION_PATH%/lib/git-core}"
    COMPLETION_PATH="$COMPLETION_PATH/share/git/completion"
    if test -f "$COMPLETION_PATH/git-prompt.sh"
    then
        . "$COMPLETION_PATH/git-completion.bash"
        . "$COMPLETION_PATH/git-prompt.sh"
        PS1="$PS1"'\[\033[36m\]'  # change color to cyan
        PS1="$PS1"'`__git_ps1`'   # bash function
    fi
fi
PS1="$PS1"'\[\033[0m\]'        # change color
PS1="$PS1"'\n'                 # new line
PS1="$PS1"'$ '                 # prompt: always $

Note: The answer using $(/bin/date) did not work for me as I think I was having trouble pathing to \Git\usr\bin\date.exe

Auroreaurous answered 9/11, 2015 at 4:50 Comment(0)
S
3

If by date you mean the current time/date, then this example can help:

 PS1="\n\[\033[35m\]\$(/bin/date)\n\[\033[32m\]\w\n\[\033[1;31m\]\u@\h: \[\033[1;34m\]\$(/usr/bin/tty | /bin/sed -e 's:/dev/::'): \[\033[1;36m\]\$(/bin/ls -1 | /usr/bin/wc -l | /bin/sed 's: ::g') files \[\033[1;33m\]\$(/bin/ls -lah | /bin/grep -m 1 total | /bin/sed 's/total //')b\[\033[0m\] -> \[\033[0m\]"

git prompt with date

It uses $(/bin/date) and is a multi-line prompt containing date/time, full path, user and host, active terminal, even file count and space usage.

It illustrates how you could integrate the date in your own git prompt.


The OP deblendewim comments:

I was also wondering how to change it without running it immediatly in the prompt.
Changed profile-file from:

if test -z "$WINELOADERNOEXEC" then PS1='\[\033]0;$MSYSTEM:\w\007 \033[32m\]\u@\h \[\033[33m\w$(__git_ps1)\033[0m\] $ ' else PS1='\[\033]0;$MSYSTEM:\w\007 \033[32m\]\u@\h \[\033[33m\w\033[0m\] $ ' 

into

if test -z "$WINELOADERNOEXEC" then PS1='\[\033]0;$MSYSTEM:\w\007 \[\033[36m\]\t \[\033[32m\]\u@\h \[\033[33m\w$(__git_ps1)\033[0m\] $ ' else PS1='\[\033]0;$MSYSTEM:\w\007 \[\033[36m\]\t \[\033[32m\]\u@\h \[\033[33m\w\033[0m\] $ ' 
Stannary answered 22/10, 2012 at 6:8 Comment(2)
Thanks VonC. The link definitly helped me out! I was also wondering how to change it without running it immediatly in the prompt. Changed profile-file from: if test -z "$WINELOADERNOEXEC" then PS1='\[\033]0;$MSYSTEM:\w\007 \033[32m\]\u@\h \[\033[33m\w$(__git_ps1)\033[0m\] $ ' else PS1='\[\033]0;$MSYSTEM:\w\007 \033[32m\]\u@\h \[\033[33m\w\033[0m\] $ ' into if test -z "$WINELOADERNOEXEC" then PS1='\[\033]0;$MSYSTEM:\w\007 \[\033[36m\]\t \[\033[32m\]\u@\h \[\033[33m\w$(__git_ps1)\033[0m\] $ ' else PS1='\[\033]0;$MSYSTEM:\w\007 \[\033[36m\]\t \[\033[32m\]\u@\h \[\033[33m\w\033[0m\] $ 'Rahel
the code I'm talking about in the above comment can be found here: C:\Program Files (x86)\Git\etc\profile (the file is called profile) somewhere around line 155Rahel
N
2

The answer given by seangwright works but it will override your global configuration. If you look inside \Git\etc\profile.d\git-prompt.sh you will see this:

if test -f ~/.config/git/git-prompt.sh
then
    . ~/.config/git/git-prompt.sh
else
    PS1='\[\033]0;$TITLEPREFIX:$PWD\007\]' # set window title
    PS1="$PS1"'\n'                 # new line
    # ...

What this means is you can override the config for just your user account, and this is probably preferred since any updates to Git Bash won't overwrite that file. All you have to do is create the ~/.config/git folder if it does not already exist and then save your configuration inside the file git-prompt.sh (full path ~/.config/git/git-prompt.sh). Going off of the most recent Git Bash configuration, it might look something like this:

PS1='\[\033]0;$TITLEPREFIX:$PWD\007\]' # set window title
PS1="$PS1"'\n'                 # new line

# THE FOLLOWING LINE ADDS THE TIMESTAMP
PS1="$PS1"'\[\033[35m\]\D{%F %T} ' 

PS1="$PS1"'\[\033[32m\]'       # change to green
PS1="$PS1"'\u@\h '             # user@host<space>
PS1="$PS1"'\[\033[35m\]'       # change to purple
PS1="$PS1"'$MSYSTEM '          # show MSYSTEM
PS1="$PS1"'\[\033[33m\]'       # change to brownish yellow
PS1="$PS1"'\w'                 # current working directory
if test -z "$WINELOADERNOEXEC"
then
  GIT_EXEC_PATH="$(git --exec-path 2>/dev/null)"
  COMPLETION_PATH="${GIT_EXEC_PATH%/libexec/git-core}"
  COMPLETION_PATH="${COMPLETION_PATH%/lib/git-core}"
  COMPLETION_PATH="$COMPLETION_PATH/share/git/completion"
  if test -f "$COMPLETION_PATH/git-prompt.sh"
  then
    . "$COMPLETION_PATH/git-completion.bash"
    . "$COMPLETION_PATH/git-prompt.sh"
    PS1="$PS1"'\[\033[36m\]'  # change color to cyan
    PS1="$PS1"'`__git_ps1`'   # bash function
  fi
fi
PS1="$PS1"'\[\033[0m\]'        # change color
PS1="$PS1"'\n'                 # new line
PS1="$PS1"'$ '                 # prompt: always $
Needleful answered 18/11, 2020 at 23:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.