gitignore does not ignore folder
GitGitignoreGit Problem Overview
In the root of my project I have a foo
folder. Inside the foo
folder I have a bar
folder. I would like to ignore all changes to all files inside my bar
folder. I have this in my gitignore
:
/foo/bar
The folder is checked: it exists and it has the files to be ignored. gitignore
is commit
ted. However, I have a file where I make a moification and is inside my bar
folder. When I type
git status
inside my git bash
I see the file which should have been ignored. What could be the reason and how can I successfully ignore all files inside my bar folder?
Note, that the files were previously ignored with the same line, but I had to temporarily remove that line to commit
something on the server. After the commit
, I put back the line into the gitignore
. This was a while ago, but now I have observed that the files will be in git status
. I would expect to be able to modify the ignored files without they appearing in the git status
.
Git Solutions
Solution 1 - Git
I'm guessing this folder has been checked into git before?
Run git rm -r --cached <folder>
and check again.
Solution 2 - Git
For me, the accepted answer was part of the solution, not the entire solution. Maybe, the other steps that I'm about to post were obvious, but I missed them first. Here's the steps I took to ensure my .gitignore
file ignored the folder I wanted it to ignore:
- Commit any changes that you need to fix/change.
- Run this command:
git rm -r --cached .
(which removes everything from the git index in order to refresh your git repository) - Then run this command:
git add .
(to add everything back to the repo) - Finally, commit these changes using
git commit -m ".gitignore Fixed"
You can find the link to the article from where I found the solution here.
Solution 3 - Git
I was having this issue and I realized git was actually ignoring the files/folders correctly, but my code editor (Visual Studio Code) was just buggy and not "greying them out" properly in the UI sidebar. I restarted VSCode and they were greyed out as expected.
Solution 4 - Git
This solved it
I had done echo node_modules >> .gitignore
and it didn't work.
the windows terminal from saves the file in UCS-2 LE BOM
and git doesn't seem to accept that.
I opened the file with Notepad
and saved with UTF-8
encoding
It Works now.
I think they need to fix this since echo "filetoignore" >> .gitignore
actually seems a handy thing to do
Solution 5 - Git
As an addition to the accepted answer
git rm -r --cached /foo/bar/
git status
When i do that, the terminal shows a bunch of rm
for files in that directory. So to prevent another commit that might unnecessarily affect remote, I i did:
git reset HEAD *
git status
After that, it said nothing to commit and when i modify files inside /foo/bar/
and do a git status
i still get nothing to commit
.
Solution 6 - Git
I created the .gitignore in powershell using echo "build" > .gitignore
. This created a file with UTF-16LE encoding. Git did not recognize the contents of the file and continued to show the build
folder as untracked. I deleted the file and recreated it in the geany text editor (UTF-8) and git now ignores the build
folder. A short search re-inforces that that git doesn't like UTF-16.