Skip to content

Instantly share code, notes, and snippets.

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

@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.

@Manouella1
Copy link

thanks man

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