Gangmax Blog

Clear Historical Commits of Octopress Master Branch

One problem when using Octopress(version “2.0”) is that: each time when doing deployment, a full copy of the website is added into the “master” branch as a new commit. If you have been updating the website hundreds of times like me, the “master” branch will have many commits and use much storage. As the content in the “master” branch is unnecessary to be version controlled, I always want to clear the “master” branch of “Octopress” to reduce the size of this repository.

Today I did some research and here is the solution(from here).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cd ~/temp
# Replace "username" with your user name.
git clone https://github.com/username/username.github.com.git
cd username.github.com
# Check out to a temporary "orphan" branch which has no commit history.
git checkout --orphan temp
# Add all the files.
git add -A
# Commit the changes.
git commit -am "Initial commit"
# Delete the old branch.
git branch -D master
# Rename the temporary branch to master.
git branch -m master
# Finally, force update to our repository.
git push -f origin master

This solution first creates a new “orphan” branch named “temp” which has no history commits, then removes the “master” branch and renames the “temp” branch to “master”, finally pushes the new “master” branch to “github.com”. After these steps the “master” branch only has one commit and all the historical commits are cleared.

Note that, in this solution if you have a local working copy, after the remote “master” branch is cleared, running “git pull” on the local copy will get the following error:

1
2
3
4
5
> git pull
Username for 'https://github.com': username
Password for 'https://username@github.com':
fatal: refusing to merge unrelated histories
exit 128

I didn’t find an elegant way to fix it. I just clone a brand new copy of the git repo to local environment again. The new cloned working copy doesn’t have this issue.

After this change, the storage used by my Octopress repository is reduced from 509MB to 147MB.

What about the storage used by the historical commits on “github.com”? I guess it will be garbage collected automatically.

Comments