1

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?

Adam Ryason
  • 539
  • 7
  • 17
  • Did you try just deleting the files on disk and leaving .gitignore? – Lasse V. Karlsen Apr 21 '22 at 17:05
  • @Lasse If we delete the files as suggested, there are no tracked changes in git. Currently, every time someone clones the repository, they have to manually delete the files. – Adam Ryason Apr 21 '22 at 17:07
  • If deleting the files from the folder doesn't tell git there are changes then there shouldn't be tracked files in there. Files are either tracked or they're not, and gitignore doesn't impact this part, gitignore only impacts the general `git add` command to start tracking them in the first place (or prevent them from being tracked). So if you delete the folder, or files therein, on disk, and git doesn't care, then they're not tracked, but then they're not in the repository either. – Lasse V. Karlsen Apr 21 '22 at 20:12

2 Answers2

2

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:

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I get an error saying "'Resources/' did not match any files". I think something like "git filter-branch" may be required. – Adam Ryason Apr 21 '22 at 17:22
  • 1
    @AdamRyason If Resources/ has to be removed from the all history, then you xwant git filter-repo (https://stackoverflow.com/a/71391674/6309) – VonC Apr 21 '22 at 17:57
  • 1
    @AdamRyason you have to give it a file path, ie `Resources/\*`. Note that the star is escaped to allow git expand it instead of your shell. – Blindy Apr 21 '22 at 17:58
2

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).

Blindy
  • 65,249
  • 10
  • 91
  • 131