Search code examples
gitgit-filter-branch

How to remove files from a folder in .gitignore?


It seems like I have an issue where a folder "Resources/" was added to .gitignore on our development branch, while another developer mistakenly committed files to this folder in a feature branch (off of development). When the feature branch was merged into the development branch, it added the files, then stopped tracking the folder and now thinks that "/Resources" and the files within it do not exist, as they should not.

The issue we're having now though, as I see from cloning the branch and switching into development, is that the files in Resources/ download, since they're added in git, but not removed.

My initial thought was to remove "/Resources" from .gitingore and then delete the files. However, if I try to remove the "/Resources" folder from .gitignore, git thinks they're new and tries to add the folder and the files that were added.

Edit: I tried to use git filter-branch, as this other post mentions but I get an error saying "fatal: bad revision 'rm'"

git filter-branch --force --index-filter 'git rm -r --cached --ignore-unmatch Resources/' --prune-empty --tag-name-filter cat -- --all

Is there a way to remove the files from a directory which are being ignored?


Solution

  • You can add a commit which acknowledges the Resource/ folder deletion, while keeping it ignored:

    git rm -r --cached Resources/
    git commit -m "Delete Resources content, now ignored"
    git push
    

    If we delete the files, there are no tracked changes in git.

    Yes, that is the point of adding Resources/ to .gitignore.

    Currently, every time someone clones the repository, they have to manually delete the files

    They won't since Resources/ is no longer track (and can be recreated locally)

    If the folder was already removed, then the developers would not have to manually delete anything: the local files would be ignored.

    If you need to remove Resources from all commits: