Despite having tried the various suggestions in this post and others (e.g. Gitignore not working and gitignore not working - being ignored?) I couldn't get git to ignore the files/folders contained within my .gitignore.txt file.
I should stress that unlike many of the previous answers that address the problem whereby a file/directory that has already been previously committed is to be untracked, in my case, I encountered this problem prior to making my first commit (i.e. having only run git init
followed by git status
).
There were a number of other promising suggestions including:
- text file encoding must be ANSI instead of UTF, e.g. Encoding -> ANSI in Notepad++
- EOL conversion for Windows, e.g. Edit -> EOL Conversion -> Windows in Notepad++
- no leading or trailing whitespaces for the items in .gitignore.txt
- no leading "/" when defining a folder
but none were applicable or resolved my issue.
After playing around with different ways of creating a .gitignore.txt file I managed to figure out how to get it to work, although I can't say that I understand why. It would be interesting to know if anyone can offer an explanation.
For the benefit of those who may also find that the previous answers don't address their issue I'll describe what I tried, what didn't work and what did. Hopefully by doing so it may also shed light on the why. Note that I'm using Windows 10.
Attempt #1 (failed)
Creating the new txt file in Windows Explorer:
- (In the project folder) Right-click -> New -> Text document
- Changed file name to ".gitignore"
- Opened file in Notepad++, entered file/directory names to be ignored
- Encoding -> ANSI [Note 1], save changes
I ran git status
but the files/folders I wanted ignored were shown as untracked. I checked the file properties in Windows Explorer. As expected the filename was ".gitignore" and the file type was "Text Document (.txt)".
Note 1:
Several contributors have stated that the encoding must be ANSI (rather than UTF). In Notepad++ I've found that regardless whether I do Encoding -> ANSI, or Encoding -> Convert to ANSI + save, close the file, and re-open it (in Notepad++), when I check the encoding it always reports UTF-8. This was also the case for the .gitignore.txt file that eventually worked.
Attempt #2 (failed)
Creating the new txt file in Notepad++:
- In Notepad++ created new file, entered items to ignore
- Saved as ".gitignore.txt", changed "Save as type" from "Normal text file" (default) to "All types", and un-ticked the box "Append extension" (ticked by default).
I ran git status
and as before all files/directories were listed under untracked files. As before the filename was ".gitignore" and the file type was "Text Document (.txt)".
Attempt #3 (succeeded)
With the same file (.gitignore.txt) still open in Notepad++:
- Saved as ".gitignore" selected "Normal text file" for "Save as type", which automatically ticked the box "Append extension"
I noticed that the file created appeared to be name-less in Windows Explorer. I checked the file's properties: the file name field was blank and the file type was "Text Document (.gitignore)". After ticking "File name extensions" in Windows Explorer (View -> File name extensions) the "name-less" file shows the ".gitignore" extension.
I ran git status
and this time the files/folders I wanted ignored were not listed as untracked files - success!
Opening the file in Notepad++ I noticed that the encoding was UTF-8, and the file name was identified as ".gitignore" (unlike in Windows Explorer).
So it appears there is subtlety in the way the txt file is created. Perhaps this is a peculiarity unique to Windows?
.gitignore
file usesANSI
orUTF-8
encoding. If it uses something else likeUnicode BOM
, it's possible that Git can't read the file. – Chitecho "file" > .gitignore
in PowerShell, the file had a UCS-2 encoding! – Kayleegit rm --cached debug.log nbproject/
– UranalysisUntracked files
list, make sure to chek out for trailing spaces (@Shermy answer) – Mcmahan/folder
. That messed me up. Should have beenfolder/
. – Josesdebug.log
,nbproject/
as well as sets the resp. files to be untracked by git ? 🤔 – Ariellatouch .gitignore
– Vicereine