Remove large file from several commits ago (in git)

9,820

Read here about filter-branch.

Someone accidentally commits a huge binary file with a thoughtless git add ., and you want to remove it everywhere. Perhaps you accidentally committed a file that contained a password, and you want to make your project open source. filter-branch is the tool you probably want to use to scrub your entire history. To remove a file named passwords.txt from your entire history, you can use the --tree-filter option to filter-branch:

$ git filter-branch --tree-filter 'rm -f passwords.txt' HEAD
Rewrite 6b9b3cf04e7c5686a9cb838c3f36a8cb6a0fc2bd (21/21)
Ref 'refs/heads/master' was rewritten

You can also do an interactive rebase onto your branch at the point where you commited those files, and remove them from that commit. The link above also explains this, but basically:

git rebase -i HEAD~X

will allow you to edit the last X commits.

Share:
9,820

Related videos on Youtube

Gentatsu
Author by

Gentatsu

Updated on September 18, 2022

Comments

  • Gentatsu
    Gentatsu over 1 year

    I had made several local commits to my codebase, and tried to push it to my repo, but there were quite a lot of large files. I just kind of gave up, and continued working, making local commits. I've deleted the large files now, and committed again, but it's still trying to push the original large files. I tried adding a gitignore in after I committed them originally to ignore any file over 50mb, to no avail.

    Any ideas?

  • Gentatsu
    Gentatsu about 7 years
    I have quite a lot of large files, though, and it seems like it'd take a while like that.
  • ladorm
    ladorm about 7 years
    I guess you mean with filter branch because of the several files. Cant you use a regex to get to them all? Then maybe the second option (rebasing) its easier cos you can go to that commit and delete the files from the system the same way you deleted them alrerady
  • Gentatsu
    Gentatsu about 7 years
    Wouldn't it have an effect on my current files, though? Would they not get changed also, or would I have to merge them?
  • ladorm
    ladorm about 7 years
    If you dont touch your files they won't change. The rebase takes your files back in history to the point of that commit and starts re-applying your commits. It stops on the commit you marked for editing. On that commit you have to remove those files, and then commit with 'git commit --ammend' (its on the link I gave you). This modifies your commit and then you resume the rebase and it starts to apply the rest of your commits
  • Gentatsu
    Gentatsu about 6 years
    Forgot about this! I did a rebase, and just squashed all my commits, which included the new ignore file, and this worked.