Search code examples
gitgitignore

.gitignore is not ignoring the files


This was the first time I used .gitignore. But it just doesn't work. The file I have entered the name of in the .gitignore file is not getting ignored. You can see that in the 'git status' result and the 'U' near the name of the file in the screenshot below, meaning the file is new and hasn't been added even once yet.

The .gitignore file is the one currently opened, it just has only one file name in it.

Image includes terminal and the .gitignore file in VS Code

This is what I get after running the git status

(.venv) PS C:\Users\thisi\CodeItAll\AutomateWithPy> git status
On branch testing
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        checking.py

nothing added to commit but untracked files present (use "git add" to track)

Here's my .gitignore file. Just copy pasted it.

checking.py


I saw symbols while searching for the solution, but the symbols had different uses, and I have entered the literal file name so the symbols are not needed.

The .gitignore file is commited with the name in it. If it was not, I would have seen it in the 'git status' result. I don't know what I'm doing wrong here.

Note: There does seem to be a space after the checking.py in the .gitignore file but I can't remove it. When I copy the entire file, I can see a bit of space. But when I go to the end of first line, and press delete, it just removes the line two which would have happened normally when there wasn't any character after the line.

P.S.: The answer is found. At least the one that was needed for me. The .gitignore file being in UTF-16 encoding was the problem. You can check the answer below.


Solution

  • I found the answer with the help of @phd's comment on the question.

    The file is UTF-16 encoded. It should be UTF-8. I tried converting the encoding from the Notepad app and saving it, but the text would change to Chinese in the VS code. And turning it back to checking.py text and saving it would convert the file back to UTF-16.

    So I removed the file, both directly and from git. Went to Notepad, created a new file, wrote the text I wanted to, and saved it in the project directly. I used the Notepad app because it gives me the option to change the encoding, I selected UTF-8 and saved it. Added the new .gitignore to the git tracking, committed the change and it works.