When GitHub Pages is served from a dedicated branch (e.g. gh-pages
), its history can be rewritten to manage repository size.
One way to do this is to simply amend the most recent commit and force push.
git commit --amend
git push --force
The upside is that previous updates no longer contribute to the repository size. The downside is that a bad commit can’t be easily undone.
To have the best of both worlds, a more sophisticated approach can be used. Instead of squashing every change into a single commit, preserve the N most recent commits and only the remaining commits.
The git-squash-range
script provided here does exactly this.
The history can be divided into two commit ranges:
-
The oldest commits to be squashed: [
root
,mid
] -
The newest commits to be preserved: (
mid
,head
]
The commit IDs for the root, mid, and head commits are determined as follows:
-
root
-git rev-list --max-parents=0 HEAD
-
mid
-git rev-parse HEAD~$N
whereN
is the number of commits we want to preserve -
head
-git rev-parse HEAD
The current branch also needs to be recorded:
-
branch
-git branch --show-current
The algorithm is then:
-
Squash the oldest commits
-
Checkout
root
git checkout $root
-
Squash merge from
mid
git merge --squash $mid
-
Amend
root
with the merge resultgit commit --amend -m squashed
-
-
Preserve the newest
N
commitsgit cherry-pick $mid..$head
-
Move the branch to the new
HEAD
-
Move the branch
git branch --force $branch HEAD
-
Checkout the branch
git checkout $branch
-