Skip to content

Instantly share code, notes, and snippets.

@pavankjadda
Last active April 24, 2025 15:39
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

@federiconoto
Copy link

It works! Thanks!

@SharakPL
Copy link

SharakPL commented Nov 7, 2021

⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️

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.

⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️

@stephpavlou
Copy link

stephpavlou commented Nov 9, 2021

@SharakPL My .gitignore is an ASCII file, and clearing the cache solved my problem.

@SharakPL
Copy link

SharakPL commented Nov 9, 2021

UTF-8 covers ascii, so switch to utf-8

@Rango989
Copy link

OMD U HERO

@fulariritesh
Copy link

Perfect solution...

@NoelleTGS
Copy link

This just isn't working no matter what I try. Every single solution online and still, git push -u origin main gives me the filesize error because the files in node_modules are too large.

@pavankjadda
Copy link
Author

@NoelleTGS don't commit node_modules to GitHub. Ignore them.

@NoelleTGS
Copy link

That's what the problem is, sorry I worded it weirdly. I'm trying to exclude that folder but it still pushes and I get the filesize error.

@SharakPL
Copy link

SharakPL commented Feb 1, 2022

@NoelleTGS Show content of your .gitignore file and make sure it's in UTF-8.

@jvencebi
Copy link

jvencebi commented Mar 2, 2022

Thank you! Worked perfect!

@Stefan2142
Copy link

This might work for the ones that committed previously new file and at some point later they dont want it in the Github https://www.deployhq.com/git/faqs/removing-large-files-from-git-history

@ozng
Copy link

ozng commented Mar 25, 2022

Also got this error lately. Turned out .gitignore file was saved with UTF-16 encoding (happened when I used npm package gitignore to generate a file). Just save it again with UTF-8 and it should work properly 😉

Thank you. :)

@jwyglendowski-precisionlender

Thanks you. This saved a ton of time. I only wasted an hour before finding this.

@MazdaX
Copy link

MazdaX commented Jun 22, 2022

Amazing solution to such an annoying issue. Specially with the git lens plugin of the VS code this had been bugging for months. Thanks a million !

@topstarwebking
Copy link

Thanks. It works for me very well

@ErnestCodes
Copy link

Thanks. It really helped

@tonyvich
Copy link

Thanks, saved my day

@taboradeyvi
Copy link

Thank you!

@raelshark
Copy link

raelshark commented Aug 20, 2022

For anyone adding any new file path or pattern to .gitignore, be sure to add a newline after the new entry at the bottom of the file if your editor doesn't automatically add it. This had me kicking myself for a while.

src\myFile.js
  <-- You need an empty line here

This is a best practice anyway and required by some languages.

@caleblent
Copy link

Thank you for this, it helped a lot!

@BradKwon
Copy link

This solved my issue. Thanks! 👍

@julortiz
Copy link

julortiz commented Sep 7, 2022

Thanks!!! You are the man.

@paladinic
Copy link

Superhero 🦸

@littlewhitedragonIN
Copy link

Perfect!!!

@iaheck
Copy link

iaheck commented Oct 23, 2022

thank you!!

@jamedkh
Copy link

jamedkh commented Nov 10, 2022

this is very helpful!

@fredrickwampamba
Copy link

Thank you so much

@MartinMarkov-QA
Copy link

Thanks for the help!

@prabhakar9885
Copy link

Thanks a lot for sharing it :)

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