Skip to content

Instantly share code, notes, and snippets.

@edthrn
Created February 10, 2021 20:15
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 edthrn/f8bd64c849e654cedd7a70412777c515 to your computer and use it in GitHub Desktop.
Save edthrn/f8bd64c849e654cedd7a70412777c515 to your computer and use it in GitHub Desktop.
Remove old data from Git repo and Git history
# Taken from https://docs.github.com/en/github/authenticating-to-github/removing-sensitive-data-from-a-repository
# Warning:
# --------
# All stashes may be lost!
PATH_TO_REMOVE=big-folder/commited/by/error
# 1. Force Git to process, but not check out, the entire history of every branch and tag
# 2. Remove the specified file, as well as any empty commits generated as a result
# 3. Overwrite existing tag
git filter-branch --force --index-filter \
"git rm -rf --cached --ignore-unmatch $PATH_TO_REMOVE" \
--prune-empty --tag-name-filter cat -- --all
git push origin -f --all
git push origin -f --tags # if necessary
# All collaborators must now REBASE their local branches on this one.
# MERGE is forbidden: it may re-introduce the files we just got rid of.
# It nothing caused unintended side-effects:
git for-each-ref --format="delete %(refname)" refs/original | git update-ref --stdin
git reflog expire --expire=now --all
git gc --prune=now
@edthrn
Copy link
Author

edthrn commented May 22, 2021

Alternative (but very similar) method described here https://stackoverflow.com/a/27745221

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