RSS

Add git commit date as last update date in hugo page

Use .GitInfo or .Page.Lastmod as last update date in hugo page

Use .GitInfo or .Page.Lastmod

Hugo have build-in support to get the last Git revision information for every content file.

To enable Hugo get git commit info, first need change enableGitInfo to true in config.toml:

Page.Lastmod is fetched from .GitInfo.AuthorDate.

enableGitInfo = true

Then in any page template can use .GitInfo or .Page.Lastmod to get current file's the latest git commit info.

Last Update:
<time datetime="{{ .Page.Lastmod.Format "Mon Jan 10 17:13:38 2020 -0700" }}" class="text-muted">
  {{ $.Page.Lastmod.Format "January 02, 2006" }}
</time>

Troubleshooting

.GitInfo is null in local hugo serve

You need commit file to make .GitInfo have validate data.

Per hugo document:

The .GitInfo feature must be enabled in your Hugo project by passing –enableGitInfo flag on the command line or by setting enableGitInfo to true in your site’s configuration file.

This is not accurate, setting enableGitInfo to true in your site’s configuration file is not enough. If you run a dev server by hugo serve, the .GitInfo also is null. You have to add --enableGitInfo to get .GitInfo:

$ hugo serve --enableGitInfo

Check the content of .GitInfo

You may use jsonify to serialize .GitInfo and display it on page:

{{ .GitInfo | jsonify }}

Here is an example output of {{ .GitInfo | jsonify }}:

{
  "hash":"f9ede6c53eed4753abe9f4ad5303a5c0fffc7b5a",
  "abbreviatedHash":"f9ede6c",
  "subject":"show last update through git commit info",
  "authorName":"...",
  "authorEmail":"...",
  "authorDate":"2020-02-13T15:01:40-08:00",
  "commitDate":"2020-02-13T15:01:40-08:00"
}

References