Possible Duplicate:
Completely remove (old) git commits from history
git is very useful for nightly snapshots of client web sites. Knowing everything (php + mysqldump + user file uploads) is in a git repository provides great peace of mind.
Due to the large size of some of the sites, I am wondering if anyone is aware a moderately easy way to remove (for example) all commits older than 30 days?
Indeed, you actually can do this. It’s a bit tricky. Here’s an example…
Now I pick out the place where I want “history to end” (for branch master, aka HEAD):
This is the last commit I will keep.
(Explanation:
git filter-branchbasically runs over all the commits in the branch, in this casemasterbecauseHEADis currentlyref: refs/heads/master, and with –parent-filter, you can rewrite the parent(s) of each commit. When we find the target commit, before which we want history to cease, we echo nothing—you don’t need the empty string, that’s my old habit from when echo with no arguments did nothing—otherwise we use “cat” to copy the existing -p arguments, as per the filter-branch manual. This makes the new commit, based off the one we tested for, have no parents, i.e., it’s now an initial commit — the root of the branch. This is unusual in a git repo, as we now have two root commits, one on the newmasterand one on the old, saved master as noted below.)Note that the older commit tree is still in the repo in its entirety, under the saved name that
git filter-branchuses:You have to delete that reference (and clean out the reflog) and do a “git gc” before the rewritten commits (and any unreferenced trees, blobs, etc) really go away:
That last line shows that they’re really gone.