Clear Git History

This site is hosted on Codeberg, which offers free hosting for open source repositories. Given those shared resources, keeping a full commit history for the site does not make sense, especially since the repo contains both the Hugo source and the generated HTML. The latter is large, and together they inflate storage and history unnecessarily.

The solution is to periodically reset history. Every couple of months, the branch is rewritten to a single initial commit that represents the current state of the site.

Steps to clear a branch’s history

Create an orphan branch from the current branch, commit the working tree, delete the original branch, rename the orphan back to the original name, then force push.

1
2
3
4
5
6
7
8
# assuming the branch is `main` and the remote is `origin`
git pull
git checkout --orphan temp-branch
git add .
git commit -am "creating new initial commit"
git branch -D main
git branch -m main
git push -f origin main

Back in the repository web UI you should see the commit count for main reset to one. Over time the on-disk size should also drop, depending on how your host reports it.

It is usually not recommended to rewrite or remove history. In this special case, a Hugo-generated site on a free, resource-constrained platform, the trade-off is worthwhile. For background on the orphan option, see the Git documentation.

Thanks for reading. If you have a lean approach for pruning large Git histories on static sites without losing useful context, share it so others can benefit.


↤ Previous Post
Next Post ↦