mrjayviper
mrjayviper

Reputation: 2388

How to implement this gitignore scenario?

TLDR: I want to ignore all files in the parent folder except contents of folder1 and folder2. In the possibility, folder1 or 2 contains jpg/png files, ignore those too.

This is what I currently have in a global .gitignore.

*.jpg
*.png
/*
!/folder1
!/folder2

Thanks a lot!

example:

/parent-folder
  -> text1.txt (ignored)
  -> text2.txt (ignored)
  ->folder1 (NOT ignored)
    ->ex-gf.png (ignored)
    ->my-resume.docx (NOT ignored)
  ->folder2 (NOT ignored)
    ->summer-vacation2017.jpg (ignored)
    ->my-grocery-list.xlsx (NOT ignored)

Upvotes: 0

Views: 90

Answers (1)

LightBender
LightBender

Reputation: 4253

.gitignore settings on folders don't apply recursively. An ignored folder won't allow anything inside it to be shown because the parent folder is missing, but the reverse is not true.

You can either explicitly exempt jpg and png files in that folder:

!/folder1/*.png
!/folder1/*.jpg
!/folder2/*.png
!/folder2/*.jpg

Exempt jpg and png files in all subfolders of folders 1 and 2

!/folder1/**/*.png
!/folder1/**/*.jpg
!/folder2/**/*.png
!/folder2/**/*.jpg

Exempt the all direct children of folders 1 and 2

!/folder1/*
!/folder2/*

Or exempt the entire content all subfolders of folders 1 and 2

!/folder1/**/*
!/folder2/**/*

You can always use git-check-ignore for testing and troubleshooting as you go

Upvotes: 1

Related Questions