Home > database >  How to remove files from a folder in .gitignore?
How to remove files from a folder in .gitignore?

Time:04-22

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?

CodePudding user response:

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:

CodePudding user response:

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.

.gitignore is used when checking new files for tracking, files you manually commited will keep being tracked regardless of what you write in that file. So the sentence above doesn't make any sense, and I believe you know that because a little later you write:

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.

Correct, those files are already part of the repository.

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

Yeah, git rm -r --cached will stage a file removal, which you can then commit with git commit and push with git push. Once the file is removed from the repository, .gitignore will again take over and the files won't be automatically staged next time (but they can be staged manually still).

  • Related