Gitignore not working

My .gitignore file isn’t working for some reason, and no amount of Googling has been able to fix it. Here is what I have:


It’s in the directory master, which is my git repo. I’m running Git because I’m on a MacBook running OSX 10.8.6.

11 Answers

The files/folder in your version control will not just delete themselves just because you added them to the .gitignore. They are already in the repository and you have to remove them. You can just do that with this:

(Remember to commit everything you’ve changed before you do this.)

git rm -rf --cached .
git add .

This removes all files from the repository and adds them back (this time respecting the rules in your .gitignore).

To untrack a single file that has already been added/initialized to your repository, i.e., stop tracking the file but not delete it from your system use: git rm --cached filename

To untrack every file that is now in your .gitignore:

First commit any outstanding code changes, and then, run this command:

git rm -r --cached .

This removes any changed files from the index(staging area), then just run:

git add .

Commit it:

git commit -m ".gitignore is now working"

After going down a bit of a bit of a rabbit hole trying to follow the answers to this question (maybe because I had to do this in a visual studio project), I found the easier path was to

  1. Cut and paste the file(s) I no longer want to track into a temporary location

  2. Commit the “deletion” of those files

  3. Commit a modification of the .gitignore to exclude the files I had temporarily moved

  4. Move the files back into the folder.

I found this to be the most straight forward way to go about it (at least in a visual studio, or I would assume other IDE heave based environment like Android Studio), without accidentally shooting myself in the foot with a pretty pervasive git rm -rf --cached . , after which the visual studio project I was working on didn’t load.

In my case it was a blank space at the beginning of the file which showed up clearly when I opened the file in Notepad, wasn’t obvious in Visual Studio Code.

I used something to generate common .gitignore for me and I ran into this. After reading @Ozesh answer I opened in VS Code because it has a nice indicator at bottom right showing type of line endings. It was LF so I converted to CRLF as suggested but no dice.

Then I looked next to the line endings and noticed it was saved using UTF16. So I resaved using UTF8 encoding an voila, it worked. I didn’t think the CRLF mattered so I changed it back to LF to be sure and it still worked.

Of course this wasn’t OPs issue since he had already committed the files so they were already indexed, but thought I’d share in case someone else stumbles across this.

TLDR; If you haven’t already committed the files and .gitignore still isn’t being respected then check file encoding and, make sure its UTF8 and if that doesn’t work then maybe try messing with line endings.

@Ahmad’s answer is working but if you just want to git ignore 1 specific file or few files do as @Nicolas suggests

step 1

add filename to .gitignore file

step 2

[remove filename (file path) from git cache

git rm --cached filename

setp 3

commit changes git add filename

git commit -m "add filename to .gitignore"

it will keep your git history clean because if you do git rm -r --cached . and add back all and commit them it will pollute your git history (it will show that you add a lot of files at one commit) not sure am I expressing my thought right but hope you get the point

In my case whitespaces at the end of the lines of .gitignore was the cause. So watch out for whitespaces in the .gitignore!

I solved my problem doing the following:

First of all, I am a windows user, but i have faced similar issue. So, I am posting my solution here.

There is one simple reason why sometimes the .gitignore doesn`t work like it is supposed to. It is due to the EOL conversion behavior.

Here is a quick fix for that

Edit > EOL Conversion > Windows Format > Save

You can blame your text editor settings for that.

For example:

As i am a windows developer, I typically use Notepad++ for editing my text unlike Vim users.

So what happens is, when i open my .gitignore file using Notepad++, it looks something like this:

## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
## Get latest from

# See for more about ignoring files.

# User-specific files
# User-specific files (MonoDevelop/Xamarin Studio)

If i open the same file using the default Notepad, this is what i get

## Ignore Visual Studio temporary files, build results, and ## files generated by popular Visual Studio add-ons. ## ## Get latest from # See for more about ignoring files. # User-specific files *.suo *.user *.userosscache 

So, you might have already guessed by looking at the output. Everything in the .gitignore has become a one liner, and since there is a ## in the start, it acts as if everything is commented.

The way to fix this is simple: Just open your .gitignore file with Notepad++ , then do the following

Edit > EOL Conversion > Windows Format > Save

The next time you open the same file with the windows default notepad, everything should be properly formatted. Try it and see if this works for you.

Also, comments have to be on their own line. They can’t be put after an entry. So this won’t work:

/node_modules  # DON'T COMMENT HERE (since nullifies entire line)

But this will work:

# fine to comment here

Adding my bit as this is a popular question.

I couldn’t place .history directory inside .gitignore because no matter what combo I tried, it just didn’t work. Windows keeps generating new files upon every save and I don’t want to see these at all.

enter image description here

But then I realized, this is just my personal development environment on my machine. Things like .history or .vscode are specific for me so it would be weird if everyone included their own .gitignore entries based on what IDE or OS they are using.

So this worked for me, just append “.history” to .git/info/exclude

echo ".history" >> .git/info/exclude

Does git reset --hard work for anyone? I am not saying this is a good solution, it just seemed to work first time I tried.

Leave a Reply

Your email address will not be published. Required fields are marked *