Hacker News new | past | comments | ask | show | jobs | submit login
Show HW: Removed large file from Git (and history too)
2 points by andrewfromx on April 9, 2023 | hide | past | favorite | 1 comment
To delete a large file from a Git repository and completely remove it from the history:

git rm --cached path/to/file

This will remove the file from the current commit but keep it in the repository history.

git commit -m "Removed large file from repository"

Now, to completely remove the file from the repository history, you need to use Git's filter-branch command. This command rewrites the repository history by applying a filter to each commit. To remove the file from all commits, run the following command:

git filter-branch --force --index-filter \

"git rm --cached --ignore-unmatch path/to/file" \

--prune-empty --tag-name-filter cat -- --all

This command will rewrite the entire history of the repository, so it may take some time to complete.

Finally, force push the changes to the remote repository to update the repository history:

git push --force

Anyone who has cloned the repository before you force push the changes will need to manually update their repository to reflect the new history.




this is great! I used to delete the entire repo and make a new one to get around accidentally checking in something huge. But it does give:

WARNING: git-filter-branch has a glut of gotchas generating mangled history

  rewrites.  Hit Ctrl-C before proceeding to abort, then use an

  alternative filtering tool such as 'git filter-repo'

  (https://github.com/newren/git-filter-repo/) instead.  See the

  filter-branch manual page for more details; to squelch this warning,

  set FILTER_BRANCH_SQUELCH_WARNING=1.
So the other option is:

git filter-repo --invert-paths --path path/to/file




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: