Git remove file from all commits
Asked Answered
H

3

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.

Huddersfield answered 24/6, 2017 at 9:54 Comment(3)
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.Coolth
Thanks @TimBiegeleisen I don't know why but it does not work for my, any way I've found #308328 with a marvelous answer.Huddersfield
This answer will help you . https://mcmap.net/q/40554/-remove-file-from-latest-commit . Basically you check out the wrong commit,remove the unwanted file,rebase all the other commitsBradfield
K
5

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/

Kaddish answered 24/6, 2017 at 10:31 Comment(0)
B
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.

Blush answered 24/6, 2017 at 10:6 Comment(1)
Couldn't agree more... Sometimes the systematic solutions in GIT are too convoluted.Fronnia
P
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.

Perfumery answered 12/2, 2022 at 17:54 Comment(1)
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.Talebearer

© 2022 - 2024 — McMap. All rights reserved.