Autodesk Autodesk - 4 years ago 326
Git Question

git filter-branch remove folder failed

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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download