Git: How to remove a file from historical commit?

I have a commit with id 56f06019 (for example). In this case, I accidentally committed a large file (50 MB). In another commit, I add the same file, but in the desired size (small). Now my repo when I clone too hard :( How do I delete this large file from the repo history to reduce the size of my repo?

+82
git
Jan 05 2018-12-12T00:
source share
3 answers

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

Let me summarize the following steps here:

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

Like the previously described reload option, filter-branch performs a rewrite operation. If you published a story, you need --force click the new links.

The filter-branch approach is significantly more powerful than the rebase approach because it

  • allows you to work with all branches / links at once,
  • renames any tags on the fly
  • works cleanly even if several merge merges have been performed since the file was added,
  • works cleanly even if the file has been re-added / deleted several times in the history of (a) branch (es)
  • It does not create new, unrelated commits, but copies them when changing the trees associated with them. This means that things like signed commits, commit notes, etc. are saved.

filter-branch also saves backups, so the repo size will not decrease immediately unless you finish collecting logs and garbage:

 rm -Rf .git/refs/original # careful git gc --aggressive --prune=now # danger 
+117
Jan 05 '12 at 11:15
source share

You will need git rebase interactively, for example, here: How to remove a commit on GitHub? and how to remove old commits .

If your commit is in HEAD minus 10 commits:

 $ git rebase -i HEAD~10 

After publishing your story, you need to click on the "new" story, you need to add + to force (see refspec in click options ):

 $ git push origin +master 

If other people have already cloned your repository, you must tell them because you just changed the story.

+1
Jan 05 2018-12-12T00:
source share

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

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

+1
Apr 11 '17 at 3:51 on
source share



All Articles