Home > Software engineering >  Advanced gitignore
Advanced gitignore

Time:06-23

I know you can use a pattern like *test* to ignore any directory or file containing word test.

But now I'm looking for something more advanced.

|- Folder A
|  |- File A
|  |- File B
|- Folder B
|  |- File C
|  |- File D
|  |- Pro.txt
|- Folder C
|  |- Pro.txt
|- Folder D
   |- File E
   |- File F

I want to ignore all folders that directly contain the Pro.txt file like Folder B & Folder C.
I do not want to ignore just the file, I want to ignore the whole folder (with subfolders and files)

The file Pro.txt is empty and just an indicator that which folders I do not wish to be uploaded on git and is not required to exist.

And before you say it, I can not change the folder name to contain a specific word.

CodePudding user response:

To expand upon @IlyaBursov's comment, git doesn't just look for .gitignore in the root of your repository -- it looks for a .gitignore file in every directory. So if you have:

.
├── Folder A
│   ├── File A
│   └── File B
├── Folder B
│   ├── File C
│   ├── File D
│   └── .gitignore
├── Folder C
│   └── .gitignore
└── Folder D
    ├── File E
    └── File F

And all those .gitignore files contain *, then running git add . in the root of the repository results in:

$ git status
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   Folder A/File A
        new file:   Folder A/File B
        new file:   Folder D/File E
        new file:   Folder D/File F

As you can see, git has ignored everything in Folder B (and in Folder C, but since that directory was empty, the .gitignore file is no-op; recall that git only tracks files, not directories).

  • Related