Why is .gitignore not ignoring my files?

GitVisual Studio-CodeGitignore

Git Problem Overview


See the image below. My .gitignore file should be ignoring all files in src/dist, but isn't.

enter image description here

Git Solutions


Solution 1 - Git

.gitignore only ignores files that are not part of the repository yet. If you already git added some files, their changes will still be tracked. To remove those files from your repository (but not from your file system) use git rm --cached on them.

Solution 2 - Git

The .gitignore file ensures that files not tracked by Git remain untracked.

Just adding folders/files to a .gitignore file will not untrack them -- they will remain tracked by Git.

To untrack files, it is necessary to remove from the repository the tracked files listed in .gitignore file. Then re-add them and commit your changes.

The easiest, most thorough way to do this is to remove and cache all files in the repository, then add them all back. All folders/files listed in .gitignore file will not be tracked. From the top folder in the repository run the following commands:

git rm -r --cached .
git add .

Then commit your changes:

git commit -m "Untrack files in .gitignore"

Please note that any previous commits with the unwanted files will remain in the commit history. When pushing to GitHub be aware of a commit history that might contain .env or client_secret.json files.

Best practice is to create a .gitignore file and populate it with the folders/files you do not want tracked when starting a project. However, often it is necessary to add to the .gitignore file after realising that unwanted files are being tracked and stored.

Solution 3 - Git

Follow These steps to work gitignore

  1. Make changes in .gitignore file.

  2. Run git rm -r --cached . command.

  3. Run git add . command

  4. git commit -m "Commit message"

Solution 4 - Git

You can use this,

git rm -r --cached ./node_modules

if you want to ignore node_modules, for example

Solution 5 - Git

If your.gitignore file isn't ignoring your files and directories.

.gitignore just ignores files that have not yet been added to the repository. If you have already git added certain files, their modifications will be tracked. Use git rm -r --cached on such files to delete them from your repository (but not from your file system).

git rm -r --cached .        #untrack files
git add .                   #re-adding the files
git commit -m "issue fixed" #commiting changes
git push                    #pushing changes

Solution 6 - Git

gitignore ignores only untracked files. Your files are marked as modified - meaning they were committed in the past, and git now tracks them.

To ignore them, you first need to delete them, git rm them, commit and then ignore them.

Solution 7 - Git

first check the .gitignore encoding.
make sure the encoding id utf-8.
then untracked unwanted file by using git rm --cached filename now your problem fixed 

Solution 8 - Git

Look at this : https://stackoverflow.com/questions/11451535/gitignore-is-not-working And particularly the remark from ADTC:

> Make sure your .gitignore file uses ANSI or UTF-8 encoding. If it uses > something else like Unicode BOM, it's possible that Git can't read the > file. – ADTC Dec 14 '17 at 12:39

Solution 9 - Git

If you are using VS Code:

Check the .gitignore file's Encoding Type. Change it to UTF-8 if it is not. Select the Reopen with Encoding option to change the file encoding.

This worked for me!

enter image description here

Solution 10 - Git

First delete the index.lock file from your git repo

rm -f .git/index.lock

and then add .gitignore

git add .gitignore

Solution 11 - Git

Its better to create .gitignore file in starting and mentioning the files we want to be ignored. If you want to ignore some files then execute git rm -r --cached

Solution 12 - Git

I was facing the same issue and then I realized that I had not created the .gitignore file properly.

In my case for some reason I created a .gitignore.txt file. Git will still track the files in the .gitignore file even after you remove the '.txt'. extension and save it.

Try deleting your old file and creating a new .gitignore file by 'touch .gitignore' in terminal, which solved it for me.

Solution 13 - Git

For some of the files in my project I needed to use git filter-branch which is better explained in this answare.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRichardView Question on Stackoverflow
Solution 1 - GitElmar PeiseView Answer on Stackoverflow
Solution 2 - GitMarckKView Answer on Stackoverflow
Solution 3 - GitßãlãjîView Answer on Stackoverflow
Solution 4 - Gitsam rView Answer on Stackoverflow
Solution 5 - GitDurgesh KumarView Answer on Stackoverflow
Solution 6 - GitIgal S.View Answer on Stackoverflow
Solution 7 - GitMohammad Sadeq SirjaniView Answer on Stackoverflow
Solution 8 - GitPhilippe RaemyView Answer on Stackoverflow
Solution 9 - GitPKPrabuView Answer on Stackoverflow
Solution 10 - GitKayVView Answer on Stackoverflow
Solution 11 - GitShivaniView Answer on Stackoverflow
Solution 12 - GitnospecView Answer on Stackoverflow
Solution 13 - GitDanilo Matrangolo MaranoView Answer on Stackoverflow