Skip to content

Instantly share code, notes, and snippets.

@oieduardorabelo
Created July 5, 2022 05:35
Show Gist options
  • Save oieduardorabelo/adf70eac0ae528a8e077a35d7362cfdb to your computer and use it in GitHub Desktop.
Save oieduardorabelo/adf70eac0ae528a8e077a35d7362cfdb to your computer and use it in GitHub Desktop.
Remove folder or file from Git history with clean up

I already had committed the folder before and want to remove the directory in the history as well.

I did the following:

Add folder to .gitignore:

echo Folder_Name/ >> .gitignore

Remove from all commits:

git filter-branch --index-filter 'git rm -rf --cached --ignore-unmatch Folder_Name/' --prune-empty --tag-name-filter cat -- --all

remove the refs from the old commits:

git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d

Ensure all old refs are fully removed

rm -Rf .git/logs .git/refs/original

Perform a garbage collection

git gc --prune=all --aggressive

push you changes to the online repository:

git push

You are done here.

But you can to the following to push all the changes to all branches with: But be careful with this command!

git push origin --all --force
git push origin --tags --force

After that the folder was removed from git, but was not deleted from local disk.

@oieduardorabelo
Copy link
Author

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