Use the --since
option to git log
to find the new start point of your history and create a new parentless commit using git commit-tree
that reuses its tree state. Afterward, rebase any children onto the new root and move your branch ref to the new HEAD.
#! /usr/bin/env perl
use strict;
use warnings;
my $MAX_AGE = 30;
my $BRANCH = "master";
# assumes linear history
my($new_start,$rebase) = `git log --reverse --since="$MAX_AGE days ago" --format=%H`;
die "$0: failed to determine new root commit"
unless defined($new_start) && $? == 0;
chomp $new_start;
my $new_base = `echo Forget old commits | git commit-tree "$new_start^{tree}"`;
die "$0: failed to orphan $new_start" unless $? == 0;
chomp $new_base;
# don't assume multiple commits more recent than $MAX_AGE
if (defined $rebase) {
system("git rebase --onto $new_base $new_start HEAD") == 0
or die "$0: git rebase failed";
}
system("git branch -f $BRANCH HEAD") == 0
or die "$0: failed to move $BRANCH";
system("git reflog expire --expire=now --all && git gc --prune=now") == 0
or die "$0: cleanup failed";
For example:
$ git lol --name-status
* 186d2e5 (HEAD, master) C
| A new-data
* 66b4a19 B
| D huge-file
* 5e89273 A
A huge-file
$ git lol --since='30 days ago'
* 186d2e5 (HEAD, master) C
* 66b4a19 B
$ ../forget-old
First, rewinding head to replay your work on top of it...
Applying: C
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (5/5), done.
Total 5 (delta 1), reused 0 (delta 0)
$ git lol --name-status
* b882852 (HEAD, master) C
| A new-data
* 63bb958 Forget old commits
Note that git lol
is a nonstandard but highly useful alias equivalent to
git log --graph --decorate --pretty=oneline --abbrev-commit
ADDITION by OP: Here's a bash version of the Perl script above:
#!/bin/bash -xe
MAX_AGE=${MAX_AGE:-30}
BRANCH=${BRANCH:-master}
# assumes linear history
{
read new_start
read rebase
} < <(git log --reverse --since="$MAX_AGE days ago" --format=%H)
[ -n "$new_start" ] # assertion
read new_base < <(
echo "Forget old commits" | git commit-tree "$new_start^{tree}"
)
# don't assume multiple commits more recent than $MAX_AGE
[ -n "$rebase" ] && git rebase --onto $new_base $new_start HEAD
git branch -f "$BRANCH" HEAD
git reflog expire --expire=now --all
git gc --prune=now
git checkout "$BRANCH" # avoid ending on "no branch"