Skip to content

Instantly share code, notes, and snippets.

@hanumantmk
Created July 20, 2017 02:07
Show Gist options
  • Save hanumantmk/b37cd664e255bc8cd182cfe1848f53b8 to your computer and use it in GitHub Desktop.
Save hanumantmk/b37cd664e255bc8cd182cfe1848f53b8 to your computer and use it in GitHub Desktop.
Example creating a git repo with a long lived feature branch and pruning a set of changes out
#!/usr/bin/perl
use strict;
use warnings;
system("rm -rf repo");
mkdir "repo";
chdir "repo";
mkdir "a";
mkdir "b";
system("touch a/a.txt");
system("touch b/b.txt");
system("git init .");
system("git add a b");
system("git commit -m 'base'");
# create a set of original history
for (my $i = 0; $i < 10; $i++) {
open A, ">>a/a.txt" or die "terribly";
open B, ">>b/b.txt" or die "terribly";
print A "orig.$i\n";
print B "orig.$i\n";
close A or die "terribly";
close B or die "terribly";
system("git add -u");
system("git commit -m 'commit orig.$i'");
}
# create a feature branch
system("git checkout -b other");
# create a bunch of history on master and the feature branch
for (my $i = 0; $i < 10; $i++) {
system("git checkout other");
for (my $j = 0; $j < 3; $j++) {
open A, ">>a/a.txt" or die "terribly";
open B, ">>b/b.txt" or die "terribly";
print A "other.$i.$j\n";
print B "other.$i.$j\n";
close A or die "terribly";
close B or die "terribly";
system("git add -u");
system("git commit -m 'commit other.$i.$j'");
}
system("git checkout master");
for (my $j = 0; $j < 3; $j++) {
open A, ">>a/a.txt" or die "terribly";
open B, ">>b/b.txt" or die "terribly";
print A "master.$i.$j\n";
print B "master.$i.$j\n";
close A or die "terribly";
close B or die "terribly";
system("git add -u");
system("git commit -m 'commit master.$i.$j'");
}
}
system("git checkout other");
system("git checkout -b other_rewritten");
# find the common merge base
my $merge_base = `git merge-base master other`;
chomp($merge_base);
# filter out all changes in the a dir for all commits on the feature branch
system("git filter-branch --tree-filter 'git checkout $merge_base -- a' ${merge_base}..HEAD");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment