Search code examples
gitgitignore

File in the same path, one can be git trace but others cannot


I have a git repo at path dir_a, and I have a file that I want to trace it at dir_a/dir_b/dir_c/memory.cpp. But I can't trace it, when I try to add it git add ./dir_b/dir_c/memory.cpp , it will announce me :

The following paths are ignored by one of your .gitignore files:
dir_b
hint: Use -f if you really want to add them.
hint: Turn this message off by running
hint: "git config advice.addIgnoredFile false"

I have check my .gitignore file in dir_a and dir_b, but I don't understand why my whilelist cannot work. My .gitignore file as follows:

#dir_a/.gitignore

*.*
*
!/dir_b/*
#dir_b/.gitignore

*.*
*
!*.cpp

And I had try to figure out which file results into this situation, and the output is as follows:

% git check-ignore -v ./dir_b/dir_c/memory.cpp
.gitignore:2:*  ./dir_b/dir_c/memory.cpp

% git check-ignore -v ./dir_b/dir_c/main.cpp

It looks like it can trace my main.cpp but cannot trace another file in the same path


Solution

  • From the gitignore docs...

    It is not possible to re-include a file if a parent directory of that file is excluded.

    * has ignored everything at the top-directory (*.* is unnecessary) including dir_b. You need to restore dir_b.

    !/dir_b/* restores what's in dir_b, but not dir_b itself. You also need to restore dir_b with !/dir_b/.

    One more issue. !/dir_b/* only restores what's in dir_b. * does not match a /; it does not restore subdirectories. For example, dir_b/subidr/foo will not be restored. For that you need !/dir_b/**, ** matches everything.

    *
    
    # Restore dir_b/
    !/dir_b/
    
    # Restore everything in dir_b/
    !/dir_b/**