The following command shows some metadata about the last git commit in json format:

git show --quiet HEAD --pretty=format:"{\"hash\":\"%h\", \"author\":\"%cn\", \"commit date\":\"%cd\"}"

{
  "hash":"0fc0fc0", 
  "author":"Adam Matan",
  "commit date":"Sun Jan 26 12:26:19 2014 +0200"}
}

Is there a way to present the date in the UTC/GMT time zone, e.g. "Sun Jan 26 10:26:19 2014" ?

有帮助吗?

解决方案 2

I don't see an utc format in the log data formats (listed this answer).

The closest I get from your format is:

git config log.date local

C:\Users\VonC\prog\git\git\>git show --quiet HEAD --pretty=format:"{\"hash\":\"%h\", \"author\":\"%cn\", \"commit date\":\"%cd\"}"
{"hash":"b594c97", "author":"Junio C Hamano", "commit date":"Thu Jan 23 10:00:28 2014 -0800"}

C:\Users\VonC\prog\git\git\>git config log.date local

C:\Users\VonC\prog\git\git\Documentation\technical>git show --quiet HEAD --pretty=format:"{\"hash\":\"%h\", \"author\":\"%cn\", \"commit date\":\"%cd\"}"
{"hash":"b594c97", "author":"Junio C Hamano", "commit date":"Thu Jan 23 19:00:28 2014"}

So from iso:

"Thu Jan 23 10:00:28 2014 -0800"

To local:

"Thu Jan 23 19:00:28 2014"

As commented, this is not UTC, unless the your machine local time is already UTC.


That was discussed on the mailing list:

Adding user.hideTimezone for setting UTC timezone

Authoring and sharing a commit by default exposes the user's time zone.

"commit --date=YYYY-MM-DDThh:mm:ss+0000" suffices to put the author time in UTC but not the commit time in UTC.
But the user shouldn't have to pass a flag at all.

Git should entirely stop accessing, recording, and sharing the user's time zone, full stop.

Failing that, git should by default stop accessing, recording, and sharing the user's time zone, but if individual users want to have their time zones on their commits, they can opt into it.

Junio C. Hamano, Git maintainer, answered:

You are free to run

$ TZ=GMT git commit

if you wanted to opt out of the feature, but this has been the default since day one and people expect Git to behave this way.

Also:

For now, using the --date argument on git commit allows you to also pass a timezone:

git commit --date="$(TZ=PST date)"

This patch (for adding a user.hideTimezone) is yet to be fully developed.

其他提示

You can use this:

TZ=UTC0 git show --quiet --date=local --format="%cd"

If you want to control the date format, you can do this:

TZ=UTC0 git show --quiet --date='format-local:%Y%m%dT%H%M%SZ' --format="%cd"

Update 2021-10-16: changed TZ=UTC to TZ=UTC0 thanks to @alex-shpilkin.

Technically git's Unix timestamp format (e.g. via --date='unix' / --format='%at') is always in UTC too. See https://git-scm.com/docs/git-log / https://git-scm.com/docs/pretty-formats for details.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top