Sometimes our .gitignore file isn’t working as expected, there are a few reasons why this might be happening. Here are a few solutions that we can apply:

Make sure that your .gitignore file is in the root directory of your repository. Git looks for this file in the root directory, so if it’s in a subdirectory, it won’t be ignored.

Sometimes we update or add files/folder before its been added to .gitignore than those files does not excluded while commiting the code.

git rm -r --cached /path-of-the-files-which-we-want-to-add-in-gitignore
git rm -r --cached . //This we remove all untracked files

git add .

git commit -m "fixed .gitignore not working issue"