Accurately displaying the last modified date on your Hugo site is easy to automate with Git integration. This guide covers how to configure Hugo so it uses your files’ Git commit history for “Last Modified” dates—no manual updates needed.
Table of Contents
Why Use Git for “Last Modified” Dates?
- You never have to remember to update lastmod in your front matter.
- Timestamps are always accurate—even if you change files outside of Hugo’s awareness.
- Maintains clean source Markdown files.
Step 1: Enable GitInfo in Hugo
Open your site’s config.toml and add:
enableGitInfo = true
This instructs Hugo to collect Git metadata for each page during hugo builds.
Step 2: Use .Lastmod in Your Templates
Update your theme’s template file (commonly single.html or a partial like date.html). Add:
Last updated: {{ .Lastmod.Format "2006-01-02 15:04 MST" }}
- .Lastmod will now automatically reflect the last Git commit date for each content file.
- The date is in UTC by default—if you want JST or another timezone, adjust the format accordingly:
Last updated: {{ .Lastmod.Format "2006-01-02 15:04 JST" }}
Step 3 (Optional): Configure Lastmod Front Matter Fallbacks
If you sometimes use front matter and want Hugo to fall back to Git, use this in config.toml:
[frontmatter]
lastmod = [":git", "lastmod", "date", "publishDate"]
This tells Hugo to:
- Prefer the Git commit date (via enableGitInfo).
- Fall back to the lastmod, date, or publishDate front matter fields if Git data is missing.
FYI: Where Is the Timestamp Stored?
- Git metadata is not added to Markdown front matter.
- The “lastmod” value is present only in the rendered HTML—nowhere else in your project or output.
Frequently Asked Questions
Q: Does this update my .md files’ front matter?
A: No. The files stay clean; Git info is injected at build time.
Q: Can I display the timezone I want?
A: Yes! Format with Hugo’s time.Format.
Q: Will it work if my content folder isn’t tracked by Git?
A: No—Hugo only finds Git commit data if the file is in your repo.
Recap
- Set enableGitInfo = true
- Use .Lastmod in your templates
- Format and display as you wish—no more manual work!
Enjoy automatic “Last Modified” timestamps with Hugo and Git.
Leave a Reply