git: timezone and timestamp format

后端 未结 8 619
遥遥无期
遥遥无期 2020-12-01 08:42

From git I can get the timestamp:

\"2011-10-04 12:58:36 -0600\"

but is there any way to show it as:

\"2011-10-04 06:58:36\"         


        
相关标签:
8条回答
  • 2020-12-01 09:17

    A full command line answer:

    TZ=GMT git show -s --format=%cd --date=iso-local
    
    0 讨论(0)
  • 2020-12-01 09:20

    jveerman's post was really helpful:

    If you want to display the git date in YYYY-MM-DD HH:MM:SS format:

    DATE=$(git log -n 1 --pretty=format:"%ad" --date=iso)
    echo "Date: ${DATE::20}"
    

    For log format I was able to add this

    [log]
    date=format:%Y-%m-%d %H:%M:%S
    

    to my ~/.gitconfig

    but getting the same nicely formatted date/time added automatically to my commit messages was an ordeal. I found nothing helpful until I added this to the .git/hooks/prepare-commit-msg file:

    DATE=$(git log -n 1 --pretty=format:"%ad" --date=iso)
    echo "${DATE::20}" >> $1
    

    If you're mainly using the Desktop app, it's lovely to have the exact time of change shown with the commit listing!

    Is there any way to make this global, so I don't have to edit each local repo's prepare-commit-msg file ?

    0 讨论(0)
  • 2020-12-01 09:21

    If you want to display the git date in YYYY-MM-DD HH:MM:SS format:

    DATE=$(git log -n 1 --pretty=format:"%ad" --date=iso)
    echo "Date: ${DATE::20}"

    0 讨论(0)
  • 2020-12-01 09:22
    git log --date=local
    

    Does the trick.

    git config --global log.date local
    
    0 讨论(0)
  • 2020-12-01 09:27
    TZ=UTC git log --date=local
    

    in order to get non-local-timezone one-timezone output.

    0 讨论(0)
  • 2020-12-01 09:29

    If you ask about git log, you can try and select most correct form from:

    git log --date={relative,local,default,iso,rfc}
    

    --date=local seems to be the best candidate.

    To make this permanent, use git config --global log.date local.

    0 讨论(0)
提交回复
热议问题