I want to remove a folder from commits on my git repo, so I use this commend:
git filter-branch --tree-filter 'rm -rf folder' HEAD
git push origin master --force
But there is no any change on my git repo, those commits which contain the folder I want to delete still on git repo.
So I try it again, but there shows:
.git-rewrite already exists please remove it
I don't know what else can I do to remove it from all commits which contain the folder on git repo permanently.
In order to remove a file or directory completely from a git repository you must ensure that it is not referenced anymore.
In git a file (or blob object) is referenced by a tree object which is referenced by either another tree object or a commit object. Commit objects are referenced by branches, tags, in the reflog and so on.
Some time ago I had to completely remove a folder from a project and wrote a blog about it. So I don't want to repeat myself here. Just take a look at Remove directories and files permanently from git page.
I also wrote a swing application that uses the jgit
library to to this. Just try GitDirStat. Making a copy of your current repo before is always a good choice.
.git-rewrite already exists please remove it
Just do what the message says. Remove .git-rewrite
.
PS: You don't have to use a --tree-filter
an index filter will do it faster ;) Then you must use git rm -rf --cached
.
Note: The operations that are presented below will override your git history. Be careful what you're doing and backup your repo if you're not sure what you're doing.
After you removed the folder via filter-branch
, like:
git filter-branch -f --tree-filter 'rm -rf folder' HEAD
and you pushed your changes to the repository (git push origin master --force
), the objects needs to be dereferenced and garbage collected.
To check what's pointing (tags or branches) to the removed objects, check the following command:
git for-each-ref --format='delete %(refname)' refs/original
Then to deference you need to expire reflog (default is 90 days) and force garbage collection, e.g.
git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
git reflog expire --expire=now --all
git gc --prune=now
then push all your branches and tags to the remote:
git push origin --force --all
git push origin --force --tags
Source: Permanently remove files and folders from Git repo
Alternatively to remove specific files or folders completely, you can use BFG Repo-Cleaner. For example:
$ bfg --delete-folders folder --no-blob-protection my-repo.git
For further details, please follow above link or Advanced Git / Remove sensitive data GitHub page.
I just did
rm -rf .git-rewrite
and it worked!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With