Gitignore all files in folders but keep folder structure

I have the following folder structure:

/foo/
/foo/test.txt
/foo/.gitkeep
/foo/bar/test.txt
/foo/bar/.gitkeep
/foo/bar/baz/test.txt
/foo/bar/baz/.gitkeep

Now I want to exclude all the files in the "foo" folder and all the files in my subfolders (and subfolders), with the exception of all .gitkeeps (to keep the folder structure). This should also work for more than three levels of subfolders.

The following gitignore rules work:

/foo/**
!/foo/**/
!/foo/**/.gitkeep

Can someone explain why this works? Is there a cleaner way to do this?

+4
source share
2 answers

The rule is simple:

Cannot re-include the file if the parent directory of this file is excluded.

That's why you need

  • ignore files and folders recursively:

    /foo/**
    

( /foo/, , <! ' , foo/ : Git )

  • :

    !/foo/**/
    
  • , .gitkeep

    !/foo/**/.gitkeep
    

, .gitkeep .

( ):

/foo/** 
!**/.gitkeep

/**, .

:

git check-ignore -v -- /path/to/.gitkeep

scm .gitignore:

  • '*' '**'
  • !.gitkeep ( -/) .

"recursively" - , , : (, . /foo/), - .
( ) (* **) gitignore ( !*), ( ) .

+3

. , .gitignore .

, , .

foo/, foo/bar/ foo/bar/baz/, .gitkeep.

.gitignore :

*
!*/
!.gitignore
!.gitkeep

[: ]

:

*
!*/
!.git*

, .

, .gitignore, - , .gitignore , , , , .

, .gitignore , . , , .gitignore. , .gitignore .

+2

Source: https://habr.com/ru/post/1685958/


All Articles