Skip to content

Instantly share code, notes, and snippets.

@pavankjadda
Last active December 17, 2025 04:20
Show Gist options
  • Select an option

  • Save pavankjadda/2bb6fbdd8786e1f57fd7bcbcc666b51d to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

It works! Thanks!

@SharakPL

SharakPL commented Nov 7, 2021

Copy link
Copy Markdown

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

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

stephpavlou commented Nov 9, 2021

Copy link
Copy Markdown

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

@SharakPL

SharakPL commented Nov 9, 2021

Copy link
Copy Markdown

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

@Rango989

Copy link
Copy Markdown

OMD U HERO

@fulariritesh

Copy link
Copy Markdown

Perfect solution...

@NoelleTGS

Copy link
Copy Markdown

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
Copy Markdown
Author

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

@NoelleTGS

Copy link
Copy Markdown

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

SharakPL commented Feb 1, 2022

Copy link
Copy Markdown

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

@jvencebi

jvencebi commented Mar 2, 2022

Copy link
Copy Markdown

Thank you! Worked perfect!

@Stefan2142

Copy link
Copy Markdown

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

ozng commented Mar 25, 2022

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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

@MazdaX

MazdaX commented Jun 22, 2022

Copy link
Copy Markdown

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
Copy Markdown

Thanks. It works for me very well

@ErnestCodes

Copy link
Copy Markdown

Thanks. It really helped

@tonyvich

Copy link
Copy Markdown

Thanks, saved my day

@taboradeyvi

Copy link
Copy Markdown

Thank you!

@raelshark

raelshark commented Aug 20, 2022

Copy link
Copy Markdown

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
Copy Markdown

Thank you for this, it helped a lot!

@BradKwon

Copy link
Copy Markdown

This solved my issue. Thanks! 👍

@julortiz

julortiz commented Sep 7, 2022

Copy link
Copy Markdown

Thanks!!! You are the man.

@paladinic

Copy link
Copy Markdown

Superhero 🦸

@littlewhitedragonIN

Copy link
Copy Markdown

Perfect!!!

@iaheck

iaheck commented Oct 23, 2022

Copy link
Copy Markdown

thank you!!

@jamedkh

jamedkh commented Nov 10, 2022

Copy link
Copy Markdown

this is very helpful!

@fredrickwampamba

Copy link
Copy Markdown

Thank you so much

@MartinMarkov-QA

Copy link
Copy Markdown

Thanks for the help!

@prabhakar9885

Copy link
Copy Markdown

Thanks a lot for sharing it :)

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