Git compare with upstream committerdate to the second

Viewed 30

I often rebase the same branch from different machines, so its not immediately clear if the branch I'm working on has the latest change and I just haven't pushed, or if that latest change was made somewhere else and I just haven't pulled.

I have a git alias to show all the local branches with upstreams and committerdates.

branc = "!f() { git for-each-ref --sort=committerdate refs/heads/$@ --format='%(color:red)%(objectname:short=8)%(color:reset) %(color:yellow)%(refname:short)%(color:reset) %(color:cyan)%(upstream:strip=2)%(color:reset) %(color:green)%(upstream:track)%(color:reset) (%(color:yellow)%(committerdate:iso-strict)%(color:reset)/%(color:cyan)%(committerdate:iso-strict)%(color:reset))'; }; f"

produces lines like

cc15e795 10496-standardise-common origin/10496-standardise-common (2022-01-26T14:07:21+02:00/2022-01-26T14:07:21+02:00)

Q1: As you can see it's not using the upstream committerdate
Q2: iso-strict is the most compact to-the-sec date format, but is there a way to ditch the timezone?

1 Answers

About the upstream's committer date:

I don't know how to do that using only for-each-ref formatting tags.

You could alwats use for-each-ref to print your ref names, and pipe this through a script which can get the upstream's committer date and print it (be wary of your color codes, though).

You may also use the --shell option to have git for-each-ref generate a shell script for you (or a perl or python script), which you can then eval or execute.

About the formatting:

There is a syntax to format your timestamp using strftime (see link to docs below), so you can choose whatever format you see fit.

Try one of the following :

# this one should use the timezone stored with each git timestamp :
--format="%(committerdate:format:%FT%T)"   # %F: full date (Y-M-D) %T: full time (H:M:S)

# this one should convert all timestamps to your env timezone before formatting :
--format="%(committerdate:format-local:%FT%T)"

# to say "show me all timestamps in UTC", you can set the 'TZ' env variable :
TZ=UTC git for-each-ref --format="%(committerdate:format-local:%FT%T)"

Check man strftime for all possible format strings (%Y, %m (<- month), %d, %H, %M (<- minutes), %S etc ...).


The doc for git help for-each-ref says (a few paragraphs below the link) :

As a special case for the date-type fields, you may specify a format for the date by adding : followed by date format name (see the values the --date option to git-rev-list(1) takes)

In git help rev-list (or git help log), you will see :

--date=<format>

[...]

--date=format:... feeds the format ... to your system strftime, except for %s, %z, and %Z, which are handled internally.

and read the details there.

Related