Home > Software engineering >  How to git ignore subfolders but not the files in first level
How to git ignore subfolders but not the files in first level

Time:07-21

I have this folder structure and I need to ignore everything except the files (.R) in the scenario1,2 folders

--workspace
      |--scenario1
           |--data (folder)
           |--outputs (folder)
           |--xx1.R
           |--xx2.R
      |--scenario2
           |--data (folder)
           |--outputs (folder)
           |--xx1.R
           |--xx2.R

I have scenario1/*/ which seems not to work

CodePudding user response:

Assuming the scenario folders are in the root of your project, scenario1/*/ isn't what you're after. That will ignore everything inside scenario1. It sounds to me that you want to ignore the data and outputs folders, but include the .R files. There are two solutions I'm providing below:

  • To ignore all subdirectories and files within the scenario folders but include the .R files (see the note below for caveats).
    • Add scenario*/*
    • Add !*.R

NOTE: The ! will re-include files/folders that were previously ignored by a matching pattern. However, the parent folder the R files are in must not be ignored. If you make a subdirectory that includes R files within the scenario folders, you'll have to use -f with git add to forcibly add them.

  • Ignoring any folders matching outputs or data but allow all other files/folders within scenario1 and scenario2
    • Add **/data/
    • Add **/outputs/

I'd go with the second option since it's less of an inconvenience for you and other contributors, assuming it still meets your requirements.

  •  Tags:  
  • git
  • Related