Strange .gitignore behavior for emacs temporary files

Viewed 4241

If I edit new_file.txt with emacs, there will be temporary files like #new_file.txt# and .#new_file.txt when the files are unsaved, and new_file.txt~ when saved. I want to exclude these files. So I write my .gitignore like this:

#This is a comment line
*~
[#]*[#]
.\#*

And this works perfectly. But later I add some comment lines to it:

#This is a comment line
*~
[#]*[#]     # this is a comment
.\#*     # this is another comment

After a git status, I see both #new_file.txt# and .#new_file.txt are listed as the untracked files.

I think .gitignore may get confused at the # character as the beginning of the comment line. So I remove those two comment lines. However, after another git status, I still see #new_file.txt# and .#new_file.txt listed as the untracked files.

I do :

git rm -r --cached .

as suggested in ".gitignore not working", but it doesn't help.

Could someone please tell me what happened, and how to make .gitignore work as I wish? Thank you very much!

1 Answers
Related