≡ Menu

.gitignore not working

I’ve run a problem several times when .gitignore doesn’t appear to be working. The file I want to ignore is specified in .gitignore, but it always comes up as an unstaged change 🙁 . I always end up searching the internet for the resolution, because I can never seem to remember it. Well today, I’m making a blog post about it, so it will be easier for me to find and hopefully help others with the same problem!

What I have discovered is that at some point in time, I mistakenly added the files that I now want to ignore to my repository. For me, this is typically a .dll in a .NET project that’s recreated on every build anyway. Since git ‘knows’ about the file, it can’t ignore it. So the resolution is simple, remove everything from git’s index and add it back which can be done with the commands below…

git rm -r --cached .

git add .

git commit -m "fixing .gitignore"

June 13, 2014 EDIT:
To just target the files listed in the .gitignore, use the command below!

for file in `cat .gitignore`  ; do git rm -r --cached $file; done
{ 32 comments… add one }
  • Sam Shiles December 23, 2010, 7:38 pm

    Perfect, just what I was looking for! Thanks

  • hen July 5, 2012, 9:23 am

    Thank you so much!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  • Dino September 10, 2012, 7:29 am

    Thank you, sir!

  • J October 19, 2012, 3:34 am

    Damn, you’re my hero 🙂

  • Andre December 29, 2012, 5:01 pm

    Yup, it worked as advertised! Thanks for the explanation.

  • Daniel April 6, 2013, 6:53 am

    This worked, thank you!

  • M July 9, 2013, 2:24 am

    Thank you very much, works like a charm!

  • Baba Krishnav September 15, 2013, 6:50 pm

    Thanks! works great… i kept forgetting the ‘.’ after ‘cached’

  • alberto November 17, 2013, 11:22 pm

    Thank you!!

  • Isaac January 9, 2014, 4:59 am

    Thanks mate!

  • Tomas May 22, 2014, 9:40 am

    Could you explain why this last commit contains a lot more files than you’d expect?
    Before I tried to fix the issue it showed like 10 files, the ones I tried to ignore.
    But in my ‘fixing gitignore’ commit are a few hundred files.
    Why are they being committed?

    • R. Kent June 13, 2014, 7:48 am

      Hi Tomas –

      Sorry for the late reply 🙂

      From my understanding,
      #git rm -r –cached .
      in effect removes everything from the repo while leaving the files in place. Since we are essentially re-adding everything with the
      # git add
      it shows all the files coming back in.

      You can directly target files to remove from the repo with something like
      #git rm -r –cached /path/to/file
      which will give you a much cleaner, and likely more appropriate solution.

      I suppose a quick way to rifle shot this would be with a command such as
      #for file in `cat .gitignore` ; do git rm -r –cached $file; done
      which should be run from the top of the repo. This command would enumerate through the files actually listed in .gitignore and remove them from the repo.

      Hope this helps!

      -Randall

  • Morgan June 1, 2014, 1:02 pm

    Thank you!

  • Anh Tran July 17, 2014, 9:34 pm

    I’ve met same problem. This fix works. Thanks.

  • Dena August 27, 2014, 5:22 pm

    Thanks for sharing! It helped me!

  • Viet September 30, 2014, 11:03 pm

    I think the better solution is
    git update-index –assume-unchanged path/to/file.txt

    because things should happen in you local repo, it should not be a commit
    Refer to: https://help.github.com/articles/ignoring-files#ignoring-versioned-files

  • LA October 7, 2014, 12:29 pm

    Great thanks!

  • Kevin Cohen October 12, 2014, 11:46 am

    This worked! You have saved me incredible amounts of frustration! Thank you!

  • Kevin Cohen October 12, 2014, 12:01 pm

    Hope you don’t mind–I posted this on Facebook and Twitter.

  • Loughlin McSweeney November 4, 2014, 8:42 am

    I find your blog post to be very informative. Make 100 dollars working from home.

    Nah, just kidding – this saved me a ton of work, funnily I couldn’t find this simple solution on Github’s support pages. I saw it referred to as a “hack” on SE but makes perfect sense to me. Thanks!

  • Tuba August 2, 2015, 10:44 am

    Saved me lots of time. Thanks!

  • Devon August 4, 2015, 7:12 pm

    Awesome. this helped me. thanks.

  • Peter Schott August 17, 2015, 4:30 pm

    This makes so much sense, though I still had to do a commit after removing all of those files using the single-line command. Still – this is restoring my sanity after I knew I had set up a .gitignore file.

  • Carl September 24, 2015, 9:31 pm

    Great post.

    Removing everything from the index/cache and adding it all back.

    Do not do the foreach method if you happen to have any ‘!filename’ entries as this will cause problems, especially if they are multiple tokens or embedded tokens.

  • Erik November 18, 2015, 3:22 pm

    Helped me a bunch. Thanks!

  • Yuval November 22, 2015, 9:46 am

    Thank you so much… A simple DuckDuckGo search brought me here, and showed me once again the real power of the internet 🙂

Leave a Comment