Skip to content

Instantly share code, notes, and snippets.

@pavankjadda
Last active April 10, 2024 15:47
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

@SharakPL
Copy link

SharakPL commented Aug 13, 2021

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 😉

@robwrc
Copy link

robwrc commented Aug 26, 2021

Excellent! Solved my problem, thanks.

@HarshitaSadadekar
Copy link

Thankss a lot.. been trying to figure this out since days and this helped me out

@erenkaradeniz
Copy link

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

It worked. Thank you!

@Daniel-Chin
Copy link

Daniel-Chin commented Sep 22, 2021

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

This is exactly my problem!! Pretty crazy. Thank you!

I created my .gitignore on Win 10 using echo 3 > .gitignore and deleted the "3". Looks like Windows > is creating files in UTF-16 with BOM nowadays...

@SharakPL
Copy link

SharakPL commented Sep 22, 2021

@Daniel-Chin yeah, I even had to have my powershell config saved with UTF-16 LE encoding. Otherwise some features wouldn't work. But it seems this was an issue with PS < 6. I see there's 7.1 already so let's try this one... 😉

7.1 works perfectly! Files are generated with proper UTF8 without BOM encoding 😃

@MyFRA
Copy link

MyFRA commented Sep 26, 2021

thanks

@mukhwanak
Copy link

perfect

@maryamshahnawaz
Copy link

It worked for me thanks 😊

@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

@martin-markov1980
Copy link

Thanks for the help!

@prabhakar9885
Copy link

Thanks a lot for sharing it :)

@BPowell76
Copy link

The UTF-8 type seemed to be the issue. Ended up having to commit to delete, roll back, copy the file and make a new one once everything was synced back up. Git seems to be ignoring the file now.

@xucian
Copy link

xucian commented Feb 25, 2023

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

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!

@Aloysius999
Copy link

Thanks! Worked a treat - saved the midnight oil :-)

@sdancer75
Copy link

Thanks !!!

@clockwiser
Copy link

Amazing! You are a hero.

@shoaibkhan404
Copy link

Easiest way

  • Delete node_modules folder
  • Add node_modules in your .gitignore file
  • Run git status it will show files to be deleted from git repo and modified .gitignore
  • Run git add .
  • Commit new changes git commit -m "Removed node_modules folder and added to .gitignore"
  • Now check repository it will not contain node_modules anymore

@Elllias
Copy link

Elllias commented Jul 25, 2023

Very bad advice for large repositories.

@fresonn
Copy link

fresonn commented Aug 14, 2023

Very bad advice for large repositories.

How else to solve the problem then?

@hope22
Copy link

hope22 commented Aug 23, 2023

Thanks dude

@nyxee
Copy link

nyxee commented Aug 30, 2023

perfect

@cheng97xng
Copy link

Perfect and thank you

@cflmarques
Copy link

Perfect, solved the exact same problem. Thanks

@AdriaPm
Copy link

AdriaPm commented Oct 18, 2023

Thank you a lot!

@vitu0089
Copy link

Man, I really need to look stuff like this up more often xD

@zft9xgy
Copy link

zft9xgy commented Nov 29, 2023

2023 and still usefull information. Thanks

@Harshitvigyos
Copy link

Thank You Bro

@simssj
Copy link

simssj commented Dec 31, 2023

I can't thank you enough. This has been driving me batshit crazy for months.

@adrianhmontero
Copy link

Ur the man!

@sahomelabs
Copy link

Thank you.

@QobilovSardor
Copy link

This solved my issue. Thanks! 👍

@KarismaYumnam
Copy link

This works! Awesome!

@nantunggaputra
Copy link

Thanks, Sir!

@adamberes
Copy link

Thanks
This works.

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