Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exclamation mark in .gitignore doesn't work

Tags:

git

gitignore

I have this file structure

aaafolder
└─ bbbfolder
   ├─ filename0.txt
   ├─ dddfolder
   │  ├─ filename1.txt
   │  └─ filename2.txt
   ├─ cccfolder
   └─ filename.txt

I want all the contents of the aaafolder folder to be hidden, but all files in the dddfolder folder must be present.

So in the repository, I want to get in Git this:

aaafolder
└─ bbbfolder
   └─ dddfolder
      ├─ filename1.txt
      └─ filename2.txt

My .gitignore looks like this:

/source/aaafolder/**
!/source/aaafolder/bbbfolder/dddfolder/*

However, the entire aaafolder folder is ignored

like image 616
mrjbom2 Avatar asked Jan 18 '21 14:01

mrjbom2


People also ask

What is exclamation mark in Gitignore?

An exclamation mark can be used to match any character except one from the specified set.

Why is my .gitignore file not working?

Check the file you're ignoring Take a good look at your structure, and make sure you're trying to ignore the file that isn't already committed to your repository. If it is, remove the file from the repository and try again. This should fix the Gitignore not working issue.

Why is my file not being ignored in Gitignore?

gitignore ignores only untracked files. Your files are marked as modified - meaning they were committed in the past, and git now tracks them. To ignore them, you first need to delete them, git rm them, commit and then ignore them.


2 Answers

Per the gitignore documentation, when negating:

it is not possible to re-include a file if a parent directory of that file is excluded.

This means that you'll need to ensure that the parent of dddfolder is included, even if the contents of dddfolder are not included.

In other words, you cannot use the ** to greedily match. You'll need to use a single-level wildcard. You'll need to exclude folders leading up to dddfolder and then include their contents.

aaafolder/*
!aaafolder/bbbfolder
aaafolder/bbbfolder/*
!aaafolder/bbbfolder/dddfolder
like image 194
Edward Thomson Avatar answered Oct 26 '22 17:10

Edward Thomson


Try it like this :

aaafolder/*
!aaafolder/bbbfolder/*
like image 32
Antoine Kurka Avatar answered Oct 26 '22 18:10

Antoine Kurka