Search code examples
gitgitignore

Trouble with ".gitignore"


I would like to ignore some particular files from committing bundle, so included them into project's ".gitignore" file:

MDK-ARM/test1_prj/
!MDK-ARM/test1_prj/*.hex
!MDK-ARM/test1_prj/*.axf
MDK-ARM/JLinkLog.txt

But unfortunately, the last line doesn't work, and consistently included in commits, can't explain why.

Another question is how to ignore changing .gitignore itself (except hiding 'em)

PS: Briefly saying I want to exclude all compiling and debugging junk except the resulting files.


Solution

  • First, make sure the file is not already tracked:

    git rm --cached -- MDK-ARM/JLinkLog.txt
    

    Second, if a folder is ignored, all the other rules regarding files inside that folder won't matter.
    It is not possible to re-include a file if a parent directory of that file is excluded.

    You need to blocklist files, not folders, before grantlisting exceptions.

    MDK-ARM/test1_prj/**
    !MDK-ARM/test1_prj/**/
    !MDK-ARM/test1_prj/*.hex
    !MDK-ARM/test1_prj/*.axf
    

    Finally, check those rules do apply with:

    git check-ignore -v -- MDK-ARM/test1_prj/a_File_Which_Should_Be_Ignored
    

    If it is ignopred, you will see the .gitignore file involved, and its ignore rule line number.