Skip to content

Instantly share code, notes, and snippets.

@pavankjadda
Last active June 20, 2024 15:28
Show Gist options
  • Save pavankjadda/2bb6fbdd8786e1f57fd7bcbcc666b51d to your computer and use it in GitHub Desktop.
Save pavankjadda/2bb6fbdd8786e1f57fd7bcbcc666b51d to your computer and use it in GitHub Desktop.
How to fix "gitignore not working" issue

FYI: Created blog post with more details

How to fix ".gitignore not working" issue?

Sometimes git does not exclude files/folders added .gitignore especially if you had commited them before. Here is how to fix it. I am ignoring node_modules from Angular project as an example

  1. Update .gitignore with the folder/file name you want to ignore. You can use anyone of the formats mentioned below (prefer format1)
### Format1  ###
node_modules/
node/

### Format2  ###
**/frontend/node_modules/**
**/frontend/node/**

  1. Commit all the changes to git. Exclude the folder/files you don't want commit, in my case node_modules
  2. Execute the following command to clear the cache
git rm -r --cached .
  1. Execute git status command and it should output node_modules and sub directories marked for deletion
  2. Now execute
git add .
git commit -m "fixed untracked files" 
  1. That's it. Comment if you any questions.

Update: Windows users make sure your .gitignore uses UTF-8 rather than UTF-16. See comment for details

@Manouella1
Copy link

thanks man

@Eazfora
Copy link

Eazfora commented May 9, 2024

thx a lot

@maornissan
Copy link

thank you!

@john9francis
Copy link

⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
Before doing possibly unnecessary commits to your project, check encoding of your .gitignore file
.gitignore should be in UTF-8
If it's not (happens for example in Powershell 5; install v.7 to fix it) then change it to UTF-8 and it should work properly.
⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️

life savior!

Spent hours trying to figure this out and it turns out my .gitignore was UTF-16 -_- Thank you for this comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment