Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git filter-branch remove folder failed

Tags:

git

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.

like image 236
Autodesk Avatar asked May 19 '15 04:05

Autodesk


3 Answers

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.

like image 57
René Link Avatar answered Nov 13 '22 06:11

René Link


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.


Git

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


Bfg

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.

like image 1
kenorb Avatar answered Nov 13 '22 08:11

kenorb


I just did

rm -rf .git-rewrite

and it worked!!

like image 1
vagdevi k Avatar answered Nov 13 '22 07:11

vagdevi k