.gitignore is ignored by Git

前端 未结 30 1344
梦毁少年i
梦毁少年i 2020-11-22 02:51

My .gitignore file seems to be being ignored by Git - could the .gitignore file be corrupt? Which file format, locale or culture does Git expect?

相关标签:
30条回答
  • 2020-11-22 03:20

    Another possible reasona few instances of Git clients running at the same time. For example, "git shell" + "GitHub Desktop", etc.


    This happened to me. I was using "GitHub Desktop" as the main client, and it was ignoring some new .gitignore settings: commit after commit:

    1. You commit something.
    2. Next, commit: it ignores .gitignore settings. Commit includes lots of temporary files mentioned in the .gitignore.
    3. Clear Git cache; check whether .gitignore is UTF-8; remove files → commit → move files back; skip one commit – nothing helped.

    Reason: the Visual Studio Code editor was running in the background with the same opened repository. Visual Studio Code has built-in Git control, and this makes for some conflicts.

    Solution: double-check multiple, hidden Git clients and use only one Git client at a time, especially while clearing the Git cache.

    0 讨论(0)
  • 2020-11-22 03:21

    Without adding another commit to your project, one line will be enough to make .gitignore work as it is supposed to:

    git rm -r --cached debug.log nbproject
    

    This will remove them from the repository, but still keep them physically. In plain English, it deletes any history of changes related to them, and also will not track their change in any future commit. You may find a better explanation here.

    0 讨论(0)
  • 2020-11-22 03:21

    Another cause of this issue is blank spaces or tabs before the statement:

    Example:

    # Be aware of the following:
     notWorkingIgnore.*
    workingIgnore.*
    

    And as pointed out by the comment below a trailing space can be an issue as well:

    # Be aware of the following:
    notWorkingIgnore.* #<-Space
    workingIgnore.*#<-Nospace
    
    0 讨论(0)
  • 2020-11-22 03:23

    I had this same problem. I believe the issue was a CR vs. CR+LF discrepancy. I stashed things in my .gitignore using CMD (on Windows 7) and the following command:

    Bad:

    echo "file_to_be_ignored.py" >> .gitignore<br>
    echo "*~" >> .gitignore
    

    Etc.

    The issue was that this command does not place the correct end-of-line marker for Git to recognize the newlines (either CR or CR+LF when Git expects the other). I solved the problem by manually replacing each newline in Vim (Vim to the rescue!) and it worked perfectly.

    Try editing your .gitignore in Notepad++ or Vim (ideally). Even if the file looks like it's formatted correctly, try replacing the newlines. It sounds weird, I know, but it worked for me. :D

    0 讨论(0)
  • 2020-11-22 03:23

    OK, so in my case the accepted solution did not work, and what worked is described here:

    Is Visual Studio 2013 ignoring your .gitignore file?

    In short:

    • Close Visual Studio.
    • Navigate to your .git folder
    • Delete ms-persist.xml
    • Restart Visual Studio
    0 讨论(0)
  • 2020-11-22 03:24

    Mine wasn't working because I've literaly created a text document called .gitignore

    Instead, create a text document, open it in Notepad++ then save as .gitignore

    Make sure to pick All types (*.*) from the dropdown when you save it.


    Or in gitbash, simply use touch .gitignore

    0 讨论(0)
提交回复
热议问题