Created
May 26, 2017 22:33
-
-
Save jcausey-astate/e16a095fb84fcc2f52109c62fe61b62f to your computer and use it in GitHub Desktop.
Modify Git repo history so that any files/folders not present in the current worktree when the script is run will be retoactively erased from history.
This file contains hidden or 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
#!/usr/bin/env bash | |
#################################################################### | |
# Removes any files that are not currently in the working tree from | |
# the git repository history. | |
#################################################################### | |
KEEPFILES=$(mktemp) | |
FULLLIST=$(mktemp) | |
REMOVEFILES=$(mktemp) | |
# Find all files in the current worktree: | |
find . | sed "s|^\./||" > ${KEEPFILES} | |
# Discard any unstaged changes | |
git reset --hard | |
# List all files in the repository now that it has reset | |
find . | sed "s|^\./||" > ${FULLLIST} | |
# Find out what files we need to remove (in the repo but not in the current tree) | |
comm -23 ${FULLLIST} ${KEEPFILES} > ${REMOVEFILES} | |
# Filter to remove any file that is in the Git `ls-files` list but NOT | |
# in the current working tree. | |
git filter-branch -f --prune-empty --index-filter "git rm -r --cached --ignore-unmatch $(cat ${REMOVEFILES} | sed 's/\(.*\)/\"\1\"/' | tr '\n' ' ') || true" | |
# Clean up | |
rm ${KEEPFILES} ${FULLLIST} ${REMOVEFILES} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment