How to Override Hugo Themes

Hugo is a very flexible static site generator with lots of nice themes to choose from. Sooner of later you will find yourself in the position where you want to modifiy parts of the them, e.g. add a git commit info to the meta info of your post or add a link to your fediverse account to the footer of each page.

You can achieve this by editing the partials files in the theme subdirectory but this will end in tears if you want to update the theme later. Especially if you install the template as a git submodule (which seems not to be the recommended way by now but I find it hard to believe that I need to install go just to install a theme in my website).

Fortunately there is a very easy way to override the templates that will be rendered. If you create a folder structure layouts/partials in the base directory of your hugo project and copy the partials you want to override into this you can modifiy them to your heart’s content without touching the original theme files.

E.g. the meta information for posts is defined in themes/PaperMod/layouts/_partials in my case using the excellent PaperMod theme. If I copy this to layouts/partials and add the following lines at the top:

{{ with .GitInfo }}
{{-  $scratch.Add "meta" (slice  "Git commit"  .AbbreviatedHash ) }}
{{ end }}

this will add the abbreviated hash of the git commit info for this post’s file to the meta data below the heading.