Jacob Goh Jacob Goh - 1 year ago 86
Git Question

how to remove previously committed file from git repo?

I accidentally committed a large zip file and pushed to bitbucket.

Now, I have already added the file to

and use
git rm --cached
to remove it and committed the deletion of the file.

But when I
git checkout
previous commit, the large zip file is still there. Meaning it's still in the git repo.

How can I remove this large zip file completely ?

Thanks in advance.

Answer Source

Because you only have one commit on top of the commit containing the large binary file, you might be able to avoid an interactive rebase.

First, nuke the second commit you made removing the large binary file:

git reset --hard HEAD~1

This leaves you in exactly the state you were when you had just committed the large binary file.

Now, at this point the large binary file should again appear locally. Delete this file, then add that change to the index if Git has not already done that for you.

Finally, amend the bad commit via:

git commit --amend

To push this branch to Bitbucket you will have to force push because you rewrote the history:

git push --force origin feature

Keep in mind that force pushing rewrites the remote history, which can cause problems for anyone else sharing this branch. In your case, the benefits of doing this probably outweight having an enormous binary being part of your history.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download