Subject: Re: [PATCH] fix git add :!x exiting with error when x is in .gitignore

From: Junio C Hamano <[email-masked]>
Date: Wed, 04 Feb 2026 10:47:02 -0800

Tian Yuchen <[email-masked]> writes:

> As you suggested, The loop needs to verify that the path matches at 
> least one positive item AND matches none of the negative items. A 
> possible way to acheive it is:
> (Notice that we no longer return 1 in the half way)
>
>  >bool matched_positive = false;
>  >
>  >for (item in pathspec) {
>  >	if (item matches patch) {
>  >		if (item is exclude) {
>  >			return 0;
>  >		} else {
>  >			matched_positive = true;
>  >		}
>  >	}
>  >}
>  >
>  >return matched_positive ? 1 : 0;

One caveat.  The case without any positive pathspec entries needs
special consideration.  I suspect, but can be totally wrong as I
didn't think things through thoroughly, that

    git add "!$y"

would want to behave as if an implicit "everything matches" was
given, i.e.,

    git add "!$y" .

while a pathspec with one or more positive entries would not need
and want such an implicit "everything" treatment.

> By the way, I think extreme cases like 'git add x :!x' should be added 
> into the test scripts.

True.