Pulling a Page's Modified Date from Git
This blog is a static site that’s generated from a series of markdown files. The ‘publish date’ for each of the blog posts is taken from a YAML frontmatter section in the post’s markdown source. Most blogging systems track when a post was last modified, and display that in the page, as well as in the page’s metadata. If I wanted to track the last modified date, it would be somewhat annoying to do manually, as I would have to edit the date in the frontmatter every time I wanted to change something in the post.
However, since my blog posts and code are stored in git, I can very easily extract the last modified date from the git repository itself. The snippet below extracts the last modified date of a file in the ISO format.
git log -1 --date=iso-strict --format="%ad" ${filename}
Edit: A Jekyll Plugin
I just found out that there is a Jekyll plugin for this purpose. It uses git log
if the site is in a Git repo, otherwise it uses File::mtime
- determinator.rb.