9

I made a "little" mistake and added a "little" (>100MB) file to my local repo.

Two commits later I'm trying to push to remote repo in github that have a limit of 100MB.

I can remove the file from my current commit with git rm --cached, but it still in previous commits.

How can I remove the file from all commits? I've tried this answer about git filter-branch but don't work for my.

Julian
  • 33,915
  • 22
  • 119
  • 174
Mquinteiro
  • 1,034
  • 1
  • 11
  • 31
  • 1
    Filter branch is the way to go AFAIK. You could also do an interactive rebase in which you remove that large file from the initial and all subsequent commits. – Tim Biegeleisen Jun 24 '17 at 09:56
  • Thanks @TimBiegeleisen I don't know why but it does not work for my, any way I've found https://stackoverflow.com/questions/307828/completely-remove-file-from-all-git-repository-commit-history with a marvelous answer. – Mquinteiro Jun 24 '17 at 10:05
  • This answer will help you . https://stackoverflow.com/a/28173964/94311 . Basically you check out the wrong commit,remove the unwanted file,rebase all the other commits – Rohith Jun 24 '17 at 10:36

3 Answers3

4

You could change the last 3 commits by interactive rebase.

git rebase -i HEAD~3

And change the commit to "edit".

See https://help.github.com/articles/about-git-rebase/

Graham
  • 7,431
  • 18
  • 59
  • 84
Julian
  • 33,915
  • 22
  • 119
  • 174
2

I would soft reset the 3 latest commits. Then remove the "little" file. Then make all the changes into 1 new commit.

It's not really ideal I think but solves the problem cause you haven't made too many additional commits yet.

lehaininh
  • 49
  • 1
  • 4
2

Concerning the observation:

I've tried this answer about git filter-branch but don't work for my.

Further details on why the linked answer doesn't work would be useful. Leaving that aside, I've successful used the code below on multiple occasions to achieve the same objective:

git filter-branch --force --index-filter \
    "git rm --cached --ignore-unmatch <your-file>" \
    --prune-empty --tag-name-filter cat -- --all

This command is broadly similar to what is being discussed in the answer that OP linked. For clarity, it is worth adding that this command was discussed in the Removing sensitive data from repository GitHub help article1.


1 It appears that article have moved so I've linked the archive.is page.

Konrad
  • 17,740
  • 16
  • 106
  • 167
  • This definitely solved my problem thank you! I like this approach as well as I was able to target the file as intended. Now I can push remotely since the file was actually removed. – JahkR Code Feb 13 '23 at 06:16