Following the “2 repo” approach of hugo site as described in previous post How to create Hugo Blog hosted on GitHub:

  1. First repo for hugo-project
  2. Second repo for the static site (public dir in hugo project is created as git submodule

I came across this approach first here.

When using this approach, I tried to do cleanup of public directory after some inital builds, because in previous builds they were many folders and files created which I no longer wanted to be present on root of generated site(these were things like multiple folders for images - later I wanted to put all images under single directory), but in subsequent builds using hugo command these files and directories were not getting removed.

My first approach was to delete the public folder and then build again using hugo command, but the problem was that the git submodule configuration also gets removed if I delete the public folder. Adding the git submodule again was tedious, had to use some force options etc.

Solution to this was to avoid deleting public folder and instead delete its contents, so as to not delete .git and .github from the public dir, but This was a lot of manual steps, considering already there are so many manual steps like

  • pushing to the first repo,
  • doing a build, and
  • pushing the second repo

So I decided to write a shell script.

But on a quick search found that people have already done it! Found this bat file that would work in windows, by ycchaos on this forum.

@echo off
	rem This script is help to deploy public from Hugo to GitPage
	rem public directory at root is generated by hugo (and is git submodule as your xxx-github-io)

	rem Put this script under your hugo site's root (near by hugo config file (config.yml or config.toml etc))
	rem -------------------------------------------------------------

	rem Input commit message
	set /P commit-message=Please input your git commit message: 
	echo Your commit message: "%commit-message%"
	echo %CD%

	rem cd to /public
	cd public
	echo %CD%

	rem Keep .git and .github workflow alive
	set "keep1=.git"
	set "keep2=.github"
	echo keep %keep1%
	echo keep %keep2%

	rem Delete others
	FOR /d %%a IN ("%CD%\*") DO IF /i NOT "%%~nxa"=="%keep2%" RD /S /Q "%%a"
	FOR %%a IN ("%CD%\*") DO IF /i NOT "%%~nxa"=="%keep1%" DEL "%%a"

	rem Back to root folder
	cd..
	echo Back to... %CD%

	rem Build hugo
	hugo

	rem Push public to GitPage repository
	cd public
	git add --all
	git commit -m "%commit-message%"
	git push
	cd..

	rem Push Hugo to source repository
	git add --all
	git commit -m "%commit-message%"
	git push

	pause

Place this file on root of your hugo project. Optionally you can add it to .gitignore

Run this batch script

  • by doublclicking the bat file
  • in powershell or gitbash by ./deploy.bat or ./deploy
    • in powershell, abort by Ctrl + C
    • Ctrl + C doesnt work to abort in gitbash, directly exit gitbash window if needed.