Search code examples
gitversion-controlgitignore

Ignore files committed to git and also remove them from history


I forgot to add certain files to my .gitignore when I began my project and consequently committed files I shouldn't have. I ignored the files afterwards as described here. The damage has already happened though since the files still exist in the history of my repository and now my repository is 10gb in size!

I have not pushed the files for the aforementioned reason, so rewriting history should be okay. In short, what I need to do is to rewrite history so that afterwards none of the files in the current .gitignore exist in any commit in the repository.

Edit: There are lots of small files contributing to the large size, so the suggested duplicate about how to remove all files above a certain size threshold does not solve this problem.


Solution

  • To reset the commit histories as original, you can use git reset --hard origin/branchname.

    To ignore files and remove them from history, you can follow below two aspects:

    1. Ignore files which already committed to git

    • Create a .gitignore file (if you don’t have) by touch .gitignore.
    • Add files and folders you want to ignore in .gitignore. The wildcard is allowed. Then commit the changes.
    • Ignore the files from committed history:

    git rm filename -r --cached git commit

    2. Remove file from commit history totally

    git filter-branch --index-filter 'git rm --ignore-unmatch -r --cached filename' --prune-empty -f -- --all
    

    To tidy your local repository (you can also skip this step):

    rm -Rf .git/refs/original
    rm -Rf .git/logs/
    git gc
    git prune --expire now
    

    To push the rewrite history to remote:

    git push -f --all