Git: How to remove file from historical commit?

Git

Git Problem Overview


I have commit with id 56f06019 (for example). In that commit i have accidentally commited large file (50Mb). In another commit i add the same file but in the right size (small). Now my repo when i clone is too heavy :( How to remove that large file from repo history to reduce the size of my repo ?

Git Solutions


Solution 1 - Git

Chapter 9 of the Pro Git book has a section on Removing Objects.

Let me outline the steps briefly here:

git filter-branch --index-filter \
    'git rm --cached --ignore-unmatch path/to/mylarge_50mb_file' \
    --tag-name-filter cat -- --all

Like the rebasing option described before, filter-branch is rewriting operation. If you have published history, you'll have to --force push the new refs.

The filter-branch approach is considerably more powerful than the rebase approach, since it

  • allows you to work on all branches/refs at once,
  • renames any tags on the fly
  • operates cleanly even if there have been several merge commits since the addition of the file
  • operates cleanly even if the file was (re)added/removed several times in the history of (a) branch(es)
  • doesn't create new, unrelated commits, but rather copies them while modifying the trees associated with them. This means that stuff like signed commits, commit notes etc. are preserved

filter-branch keeps backups too, so the size of the repo won't decrease immediately unless you expire the reflogs and garbage collect:

rm -Rf .git/refs/original       # careful
git gc --aggressive --prune=now # danger

Solution 2 - Git

You can use git-extras tool. The obliterate command completely remove a file from the repository, including past commits and tags.

https://github.com/tj/git-extras/blob/master/Commands.md

Solution 3 - Git

I tried using the following answer on windows https://stackoverflow.com/a/8741530/8461756

Single quote does not work on windows; you need double-quotes.

Following worked for me.

git filter-branch --force --index-filter "git rm --cached --ignore-unmatch PathRelativeRepositoryRoot/bigfile.csv" -- --all

After removing the big file, I was able to push my changes to GitHub master.

Solution 4 - Git

You will need to git rebase in the interactive mode see an example here: https://stackoverflow.com/questions/448919/how-can-i-remove-a-commit-on-github and how to remove old commits.

If your commit is at HEAD minus 10 commits:

$ git rebase -i HEAD~10

After the edition of your history, you need to push the "new" history, you need to add the + to force (see the refspec in the push options):

$ git push origin +master

If other people have already cloned your repository, you will to inform them, because you just changed the history.

Solution 5 - Git

You can use a simple Command to deleted

 git rm -r -f app/unused.txt 
 git rm -r -f yourfilepath

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmariooshView Question on Stackoverflow
Solution 1 - GitseheView Answer on Stackoverflow
Solution 2 - GitOfer SegevView Answer on Stackoverflow
Solution 3 - GitSandeep DixitView Answer on Stackoverflow
Solution 4 - GitLoïc d'AnterrochesView Answer on Stackoverflow
Solution 5 - Gitmini developerView Answer on Stackoverflow