Skip to content

Instantly share code, notes, and snippets.

@michaelchughes
Last active April 7, 2023 15:02
Show Gist options
  • Save michaelchughes/8798a51ec5c9ea2c575bc5ef5674c1eb to your computer and use it in GitHub Desktop.
Save michaelchughes/8798a51ec5c9ea2c575bc5ef5674c1eb to your computer and use it in GitHub Desktop.
Remove Bad Files from Git Completely

RECIPE TO TOTALLY REMOVE BAD FILES FROM A GIT REPO WITH MULTIPLE USERS

1) Make sure all local copies of the repo are sync'd on the same commit

We assume that there are two users (A and B). A is gonna delete the bad files. B is gonna receive the changes.

At the start, we need A and B to have the SAME version history.

We'll assume there are no branches other than master.

2) User A (in charge of deletion) should do this for each bad file:

git filter-branch --force --index-filter
'git rm --cached --ignore-unmatch relative/path/to/bad_file.txt'
--prune-empty --tag-name-filter cat -- --all

Source: https://stackoverflow.com/questions/2004024/how-to-permanently-delete-a-file-stored-in-git

3) User A should then push

git push --force --all

4) User B should then fetch changes

git fetch

5) User B should verify that the changes have been fetched:

git status

Expected output: Message showing origin/master has diverged.

On branch master
Your branch and 'origin/master' have diverged,
and have 3 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)
nothing to commit, working tree clean

6) User B should then RESET

git reset --hard origin/master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment