如何通过添加日期时间来配置 Git bash 提示?

deb*_*wim 5 msysgit git-bash

我的 msysgit Git bash 命令行提示符现在看起来像这样:

GitUserName@WorkStationName WorkSpacePath (BranchName)
Run Code Online (Sandbox Code Playgroud)

我想在该行前面有一个时间戳,例如HH:mm( hours:minutes)。

有谁知道我如何轻松做到这一点?

sea*_*ght 5

我发现使用该文件,\Git\etc\profile.d\git-prompt.sh您可以添加如下所示的行以将日期时间添加到您的提示中

PS1="$PS1"'\[\033[35m\]\D{%F %T} '(参见此处的格式详细信息)

我的提示目前看起来像这样

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 $
Run Code Online (Sandbox Code Playgroud)

注意:答案 using$(/bin/date)对我不起作用,因为我认为我无法找到\Git\usr\bin\date.exe


Von*_*onC 3

如果日期指的是当前时间/日期,那么这个例子可以帮助:

 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\]"
Run Code Online (Sandbox Code Playgroud)

带日期的 git 提示符

它使用$(/bin/date)并且是一个多行提示符,包含日期/时间、完整路径、用户和主机、活动终端,甚至文件计数和空间使用情况。

它说明了如何将日期集成到您自己的 git 提示符中。


OP deblendewim 评论

我还想知道如何更改它而不立即在提示符中运行它。
更改了配置文件:

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\] $ ' 
Run Code Online (Sandbox Code Playgroud)

进入

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\] $ ' 
Run Code Online (Sandbox Code Playgroud)