.gitignore is ignored by Git

前端 未结 30 1386
梦毁少年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:25

    All the answers here are actually workarounds. You need to create the .gitignore file before you run git init. Otherwise git will never know you need to ignore those files, because they have been tracked already.

    echo .idea/ >> .gitignore
    git init
    

    If you develop on a daily basis, I advise you to add your habitual ignored files to your ~/.gitignore_global file. That way, git will already know which files you (meaning "your user", since it's a file in your home directory) usually ignore.

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

    I've created .gitignore using echo "..." > .gitignore in PowerShell in Windows, because it does not let me to create it in Windows Explorer.

    The problem in my case was the encoding of the created file, and the problem was solved after I changed it to ANSI.

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

    It is also a possibility that you edited the .gitignore file with a sudo command. I encountered the same issue and while executing the commands: git status, I could still see the "should be ignored" files.

    Upon editing with nano .gitignore instead of sudo nano .gitignore, I could see the correct reflection.

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

    There's another issue with .gitignore that might happen, especially for a Windows user. Git does not like it when you name .gitignore (such as unity.gitignore).

    You'll want to always name it .gitignore, or on Windows, .gitignore. as Windows thinks you are trying to rename it without a filename.

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

    I noticed that the encoding of the .gitignore was having an effect--if the file was Unicode, it was ignored, if it was ASCII, it wasn't.

    Process:

    1. Verify status: PS> git status
    2. Create a function to Get-FileEncoding
    3. Test .gitignore's encoding: PS> Get-FileEncoding .gitignore
    4. Change the encoding to ASCII: PS> Set-Content .gitignore -Encoding Ascii -Value (Get-Content .gitignore)
    5. Confirm: PS> git status
    0 讨论(0)
  • 2020-11-22 03:33

    Also check out the directory where you put .gitignore.

    It should be in the root of your project:

    ./myproject/.gitignore
    

    Not in

    ./myproject/.git/.gitignore
    
    0 讨论(0)
提交回复
热议问题