File added in .gitignore but still shows up when I switch to another branch as untracked file

The branch I switched to doesn’t contain the file I added in the original branch. When I gitignore it, the file still shows up as an untracked file on git status of the other branch, even though this branch doesn’t contain this file.

In other words, the ignored files of the original branch show up as untracked files when I switch to a branch that doesn’t have those files. If I remove those files from the branch I switch to, they are removed as well in the original branch.

Any help?

I’m confused about which branch(s) has your .gitignore, the origin of the files and how files that don’t exist in a given branch appear in that branch (that shouldn’t be possible)

Generally .gitignore should be included in each of your branches where you want to “ignore” files for, following the usual syntax.

One common gotcha, which may or may not apply here, is committing files that are then later ignored via .gitignore. This tells git to ignore files moving forward, but does not clear up the history of a file, or even keeps track if you delete a file later, as it’s already “in the history”.

This “the file is ignored but not deleted!” problem is rather common if you’re committing files and not paying attention.

There are a few solutions on how to “purge” your commit history to clean this up, but the simplest may be to just wipe out the history you already have. In this case if your issue arises between branches,