Removing files from .git folder that you thought you deleted
git filter-branch --index-filter 'git rm -rf --cached --ignore-unmatch FOLDER_NAME' --prune-empty --tag-name-filter cat -- --all
```
- The filter-branch command allows you to rewrite the Git history.
- The –index-filer option is the filter for rewriting the index. Some people use the –tree-filter option, which rewrites the tree and its contents, but the –index-filter is faster because it does not check out the tree.
- This option is used with git rm -rf –cached –ignore-unmatch for optimal results, which removes (rm) the file recursively and forcefully (-rf).
- –cached is used to unstage and remove paths from the index.
- –ignore-unmatch will prevent the command from failing if the file is absent from the tree of a commit.
- –prune-empty allows the filter-branch command to ignore empty commits generated by the filters applied.
- –tag-name-filter cat will update the relevant tags by rewriting them.
- – simply separates the filter-branch options from the revision options and –all will rewrite ALL branches and tags
git prune
- This prunes all unreachable objects from the object database.
rm -rf .git/refs/original/
- This removes any old references to the unwanted folder/file.
git reflog expire --expire=now --all
- According to the documentation, reflog is the mechanism to record when the top of branches are updated so git reflog manges the information recorded. expire is used to prune older reflog entries and `–expire=now specifies how far behind these older entries should be, in this case, right now.
git gc --prune=now
- This command cleans up unnecessary files and optimises the local repository. –prune=now prunes objects older than the date specified, in this case, right now.
After all that, I cloned this into a new local repo using:
git clone --no-hardlinks file://PATH/TO/OLD-REPO NEW-REPO
This gave me a “clean” repository, with the history rewritten to remove the target folder, but with all the other commits still intact.
uuid: 202006172328 tags: #programming