Skip to content

Instantly share code, notes, and snippets.

@bhcleek
Last active September 3, 2015 15:49
Show Gist options
  • Save bhcleek/94102a68b818f81915eb to your computer and use it in GitHub Desktop.
Save bhcleek/94102a68b818f81915eb to your computer and use it in GitHub Desktop.
extract multiple files' histories from git

Extracting the history of a set of files that are spread across multiple subtrees, but are not composed wholly of those subtrees is possible with git, but not always easily grokked. It's not terribly difficult, however; two lines of shell script can get it done. First, use git-filter-branch to extract the history of the files. This, however, may leave you with merge commits that make no changes, because --prune-empty will not prune empty merge commits. A second filter branch, though, can remove those empty merge commits.

git filter-branch -f --prune-empty --index-filter 'git rm --cached --ignore-unmatch $(git ls-files | grep -v "path/to/first/file\|LICENSE")'
git filter-branch -f --prune-empty --parent-filter 'sed -e "s/-p//g" > parents; git show-branch --independent $(cat parents) | sed -e "s/^/ -p /" -e "1 s/^ //" -e "s/^-p$//" | tr "\n" " " | tee -a all-parents; rm -f parents'

Thanks to http://git.661346.n2.nabble.com/Removing-useless-merge-commit-with-quot-filter-branch-quot-td7356544.html for the basic outline the second step.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment