151

I tried to remove a file from my remote repo by running:

git filter-branch --index-filter 'git rm --cached --ignore-unmatch Rakefile' HEAD

But Git complains that

Cannot create new backup. A previous backup already exists in refs/original/
Force overwriting the backup with -f
rm: cannot remove /.git-rewrite/backup-refs : Permission denied
rm: cannot remove directory /.git-rewrite : Directory not empty

This was after I already deleted the .git-rewrite directory on Windows.

How can I remove that file? It's a 29Mb file sitting on my repo, so I quite need to remove the file.

I tried to delete the commit in git rebase -i, but apparently because the commit touched a lot of different files, Git complains of conflicts and I aborted to be safe.

Roberto Tyley
  • 24,513
  • 11
  • 72
  • 101
Cardin
  • 5,148
  • 5
  • 36
  • 37
  • 1
    For search engines: this may also apply when your error message is `.git-rewrite already exists, please remove it`. – Drew Noakes Jun 19 '15 at 00:46

4 Answers4

274

You have already performed a filter-branch operation. After filter-branch, Git keeps refs to the old commits around, in case something goes wrong.

You can find those in .git/refs/original/…. Either delete that directory and all files within, or use the -f flag to force Git to delete the old references.

git filter-branch -f \
--index-filter 'git rm --cached --ignore-unmatch Rakefile' HEAD
knittl
  • 246,190
  • 53
  • 318
  • 364
  • 11
    Finally got it to work, thanks! I did try the -f flag, but the problem was I was putting it at the end of the command eg. HEAD -f. Seeing your command made me try putting the flag at the start, and it worked! =D – Cardin Jun 20 '11 at 00:02
  • 12
    option flags (`-f`) go before the refs (`HEAD`). refs go last – knittl Jun 20 '11 at 05:52
  • I'm still getting the same error after adding -f AND deleting the directory. Any ideas? – Yaron Apr 23 '15 at 23:26
  • @Yaron: which error? `git add` doesn't show up in my answer. – knittl Apr 24 '15 at 05:31
  • 1
    @knittl, i wasn't referring to `git add`. I was receiving the same error as the original poster even after deleting the `.git/refs/original/` folder and using the `-f` flag in the `git filter-branch` command. The solution, in my case, was to delete the `.git/packed-refs` file. – Yaron Apr 25 '15 at 19:58
  • @Yaron: `packed-refs` contains all references. You might have deleted a lot of important references. – knittl Apr 25 '15 at 21:35
  • I am getting this same problem with permissions, even I did the `filter-branch` for the *first* time! Any ideas? Repository was actually copied (fetched) from the remote repository. – Gabrielius Nov 04 '15 at 19:22
  • @Gabrielius: are the file and directory permissions in your repository correct? Can you write to your disk, is still disk space available? Also make sure the `.git-rewrite` directory does not exist. – knittl Nov 05 '15 at 06:28
  • 1
    @knittl, I found how to fix it, but did not find what the problem was. Basically, I added `--ignore-unmatch` to command `git rm` and it went smoothly! – Gabrielius Nov 12 '15 at 18:40
  • @Gabrielius: that's already contained in my answer since 2012? :) – knittl Nov 13 '15 at 00:05
  • @knittl, yeah funny. The problem started when I removed `--ignore-unmatch` because with it nothing was deleted, but then this permissions error appeared. I then re-added the parameter, but used it with a *different path* this time. Thus the real issue was the wrong path :) – Gabrielius Nov 13 '15 at 08:37
  • Deleting `.git/refs/original/` did not solve my issue. I restored the folder, then used [this answer](https://stackoverflow.com/a/34177510/4080966) to remove the backup. – Erik Koopmans May 01 '18 at 05:51
  • For file path with space in the name use this: git filter-branch -f --index-filter "git rm -rf --cached --ignore-unmatch 'large file.pdf'" HEAD – user160357 Sep 02 '22 at 22:07
29

Use this command to remove original backup:

git update-ref -d refs/original/refs/heads/master

Here is gist I used to filter-branch my git repo: https://gist.github.com/k06a/25a0214c98bc19fd6817

k06a
  • 17,755
  • 10
  • 70
  • 110
5

I had the same problem and the answer above didn't fix it. There was no .git/refs/original/ directory left. The solution for me was to delete the .git/packed-refs file.

Yaron
  • 1,867
  • 20
  • 16
  • Same problem. No `.git/packed-refs` file, however, and no folder `original` in `.git/refs`. Anyone? – XedinUnknown Jun 25 '15 at 13:00
  • Resolved [here](http://stackoverflow.com/questions/18746580/removing-history-from-git-git-command-fails) by adding the `-r` flag to command. – XedinUnknown Jun 25 '15 at 13:30
  • Same problem here. Deleting packed-refs solved the problem, but it also ruined the rest of my git directory, and I had to restore from backup. – Kevin Yin Apr 06 '17 at 20:46
2

Add a force to the filter branch command.

Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141