Skip to content

Instantly share code, notes, and snippets.

@jennings
Last active January 10, 2019 23:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jennings/f3b3f24285e38650c74c0b0d2db8bb77 to your computer and use it in GitHub Desktop.
Save jennings/f3b3f24285e38650c74c0b0d2db8bb77 to your computer and use it in GitHub Desktop.
Using the `--state-branch` option to `git-filter-branch` to incrementally filter a branch
#!/bin/bash
set -exuo pipefail
TRUNK=master
REWRITE_BRANCH=master-to-rewrite
NEW_BRANCH_BOOKMARK=master-filtered
LAST_IMPORT_BRANCH=master-lastimport
STATE=master-state
INDEX_FILTER_SCRIPT='git rm -r --cached --ignore-unmatch bad_directory'
# Update the trunk
git checkout -f $TRUNK
git clean -fd || true
git pull
# Prepare the branch to rewrite
git branch -D $REWRITE_BRANCH || true
git branch $REWRITE_BRANCH $TRUNK
# Go!
git filter-branch --index-filter "$INDEX_FILTER_SCRIPT" \
--state-branch $STATE \
--prune-empty \
-f \
$LAST_IMPORT_BRANCH..$REWRITE_BRANCH
git checkout $NEW_BRANCH_BOOKMARK
git merge --ff-only $REWRITE_BRANCH
# Cleanup
git checkout -f $LAST_IMPORT_BRANCH
git merge --ff-only $TRUNK
git checkout $TRUNK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment