git except a sub directory and it's files of a ignored directory

后端 未结 3 742
粉色の甜心
粉色の甜心 2021-01-15 21:39

i use git to manage my sources,i have some file in bellow paths:

Debug/a.dll
Debug/b.exe
Debug/c.png
Debug/Pic/a.png
Debug/Pic/b.bmp
Debug/Pic/c.dll
<         


        
相关标签:
3条回答
  • 2021-01-15 22:07

    Git use the .gitignore to ignore or to track files in ignored paths.

    In your case you need to do add this to your .gitignore file in your project root directory. Create the file is it does not exist

    #List of files to ignore
    Debug/*
    
    #List of files to exclude from the ignored pattern above
    !Debug/Pic
    !Debug/Pic/*
    

    What is in content of this sample .gitignore

    Debug/* - This will ignore all the files under the Debug folder
    !Debug/Pic/* - The ! is a special character in this case telling git to exclude the given pattern from the ignored paths.

    In other words:
    We "told" git to ignore all the files under the Debug folder but to include all the files under the Debug/Pic/ folder.

    0 讨论(0)
  • 2021-01-15 22:14

    As usual, with exclusion in gitignore, the rule to remember is:

    It is not possible to re-include a file if a parent directory of that file is excluded (*)
    (*: unless certain conditions are met in git 2.?, see below)

    Since Debug/* would ignore the folder Debug/Pic/, trying to exclude the content of that folder would not work (!Debug/Pic/*) with git 2.6 or less.

    You need to exclude the folder first. Then its content.

    Debug/*
    !Debug/Pic/
    !Debug/Pic/*
    

    Note that with git 2.9.x/2.10 (mid 2016?), it might be possible to re-include a file if a parent directory of that file is excluded if there is no wildcard in the path re-included.

    Nguyễn Thái Ngọc Duy (pclouds) is trying to add this feature:

    • commit 506d8f1 for git v2.7.0, reverted in commit 76b620d git v2.8.0-rc0
    • commit 5e57f9c git v2.8.0-rc0,... reverted(!) in commit 5cee3493 git 2.8.0-rc4.

    So in git 2.9+, in order to re-include the the folder Debug/Pic/, you can do:

    /Debug
    !Debug/Pic
    
    0 讨论(0)
  • 2021-01-15 22:19

    You can add the subdirectory first, then ignore the containing directory:

    git add Debug/Pic
    git ignore Debug
    

    This will have the side effect of not showing the addition of new files to Debug/Pic, but you can just add them manually with 'git add -f' to get around the .gitignore warning.

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