Last active
August 29, 2015 13:56
-
-
Save bgerstle/8979026 to your computer and use it in GitHub Desktop.
Rewrite git history by removing everything except desired files, which are represented by a line-separated list of regex patterns in ~/file_patterns.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
git filter-branch --tree-filter \ | |
'find . \ | |
\( -name ".gitignore" -o -path "./.git*" \) -prune -o \ | |
\( -type f -o -type l \) -exec echo \"{}\" ";" \ | |
| grep -vE -f ~/file_patterns \ | |
| xargs rm \ | |
&& find . -empty -delete' \ | |
--prune-empty HEAD | |
# 1: rewrite git history by running the following command for each commit | |
# --- Command Start --- | |
# 2: breadth-first traversal of current directory | |
# 3: ignore (prune) `.gitignore` file and `.git` folder | |
# 4: echo any files or links surrounded by quotes (to prevent grep from getting tripped up by paths w/ spaces) | |
# 5: filter out the paths echoed by `find` which match the patterns in "~/file_patterns" | |
# 6: split outputs of `grep` to multiple calls of `rm` (delete the all files but those that match a line in "~/file_patterns") | |
# 7: find and delete any directories that are now empty | |
# --- Command End --- | |
# 8: prune any empty commits and run on the branch HEAD |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment