.gitignore is ignored by Git

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

    There's some great answers already, but my situation was tedious. I'd edited source of an installed PLM (product lifecycle management) software on Win10 and afterward decided, "I probably should have made this a git repo."

    So, the cache option won't work for me, directly. Posting for others who may have added source control AFTER doing a bunch of initial work AND .gitignore isn't working BUT, you might be scared to lose a bunch of work so git rm --cached isn't for you.

    !IMPORTANT: This is really because I added git too late to a "project" that is too big and seems to ignore my .gitignore. I've NO commits, ever. I can git away with this :)

    First, I just did:

    rm -rf .git
    rm -rf .gitignore
    

    Then, I had to have a picture of my changes. Again, this is an install product that I've done changes on. Too late for a first commit of pure master branch. So, I needed a list of what I changed since I'd installed the program by adding > changed.log to either of the following:

    PowerShell

    # Get files modified since date.
    Get-ChildItem -Path path\to\installed\software\ -Recurse -File | Where-Object -FilterScript {($_.LastWriteTime -gt '2020-02-25')} | Select-Object FullName
    

    Bash

    # Get files modified in the last 10 days...
    find ./ -type f -mtime -10
    

    Now, I have my list of what I changed in the last ten days (let's not get into best practices here other than to say, yes, I did this to myself).

    For a fresh start, now:

    git init .
    # Create and edit .gitignore
    

    I had to compare my changed list to my growing .gitignore, running git status as I improved it, but my edits in .gitignore are read-in as I go.

    Finally, I've the list of desired changes! In my case it's boilerplate - some theme work along with sever xml configs specific to running a dev system against this software that I want to put in a repo for other devs to grab and contribute on... This will be our master branch, so committing, pushing, and, finally BRANCHING for new work!

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

    One thing to also look at: Are you saving your .gitignore file with the correct line endings?

    Windows:

    If you're using it on Windows, are you saving it with Windows line endings? Not all programs will do this by default; Notepad++ and many PHP editors default to Linux line endings so the files will be server compatible. One easy way to check this, is open the file in Windows Notepad. If everything appears on one line, then the file was saved with Linux line endings.

    Linux:

    If you are having trouble with the file working in a Linux environment, open the file in an editor such as Emacs or nano. If you see any non-printable characters, then the file was saved with Windows line endings.

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

    Specifically for Windows users: If you have untracked files and clearing/removing the cached files is not working. Try opening PowerShell and converting the .gitignore file to UTF-8 encoding:

    $Myfile = Get-Content .\.gitignore`
    $Myfile | Out-File -Encoding "UTF8" .gitignore
    

    You need to only do this once to encode the .gitignore file for that directory, and since the file is then encoded correctly, whenever you edit the file in the future it should work. I believe this is due to a glitch with GitHub not being about to read non UTF-8 encoding for a .gitignore file. As far as I'm aware this issue has not yet been resolved for Windows. It's not too big of a deal, just a pain to debug when it's not working.

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

    For me it was yet another problem. My .gitignore file is set up to ignore everything except stuff that I tell it to not ignore. Like such:

    /*
    !/content/
    

    Now this obviously means that I'm also telling Git to ignore the .gitignore file itself. Which was not a problem as long as I was not tracking the .gitignore file. But at some point I committed the .gitignore file itself. This then led to the .gitignore file being properly ignored.

    So adding one more line fixed it:

    /*
    !/content/
    !.gitignore
    
    0 讨论(0)
  • 2020-11-22 03:20

    Even if you haven't tracked the files so far, Git seems to be able to "know" about them even after you add them to .gitignore.

    WARNING: First commit or stash your current changes, or you will lose them.

    Then run the following commands from the top folder of your Git repository:

    git rm -r --cached .
    git add .
    git commit -m "fixed untracked files"
    
    0 讨论(0)
  • 2020-11-22 03:20

    In my case, it's because the files already exist in the repository and I'm trying to ignore it.

    These are the things I did to fix the issue:

    • Copy the files to a temporary folder
    • Remove them from my project folder.
    • Commit the changes which remove those files from the repository
    • Re-added those files to my project folder

    By then, any changes I made on those files were ignored.

    I think you can't ignore files that already exist on the repository.

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