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
Perfect, just what I was looking for! Thanks
Thank you so much!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Thank you, sir!
Damn, you’re my hero 🙂
Yup, it worked as advertised! Thanks for the explanation.
This worked, thank you!
Thank you very much, works like a charm!
Thanks! works great… i kept forgetting the ‘.’ after ‘cached’
Thank you!!
Thanks mate!
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?
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
Thank you!
I’ve met same problem. This fix works. Thanks.
Thanks for sharing! It helped me!
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
Great thanks!
This worked! You have saved me incredible amounts of frustration! Thank you!
Hope you don’t mind–I posted this on Facebook and Twitter.
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!
Saved me lots of time. Thanks!
Awesome. this helped me. thanks.
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.
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.
Helped me a bunch. Thanks!
Thank you so much… A simple DuckDuckGo search brought me here, and showed me once again the real power of the internet 🙂