# git clean deletes negated pathspec if directory causing data loss

5 messages from 2025-10-05 to 2025-10-30. Participants: Devste Devste, Johannes Schindelin, Junio C Hamano.
Thread: https://gitlist.dev/t/64251

## Devste Devste, 2025-10-05 11:27

Subject: git clean deletes negated pathspec if directory causing data loss
Message-ID: <CANM0SV0R7OYJKeBAs5-WY2Wqp7VxhKv5=LQ4cuDd6MaQwNqGNw@mail.gmail.com>
URL: https://gitlist.dev/e/CANM0SV0R7OYJKeBAs5-WY2Wqp7VxhKv5%3DLQ4cuDd6MaQwNqGNw%40mail.gmail.com

```
I am using:
git version 2.51.0.windows.1

Run:
echo .idea/dictionaries >> .gitignore
mkdir -p .idea/dictionaries
touch .idea/dictionaries/foo.xml
git clean -f -f -d -X --dry-run -- ':!/.idea/dictionaries' ':*.rej' ':/*/*.log'

Outputs:
Would remove .idea/dictionaries/

No matter how you specify the pathspec (':!.idea',...) it always wants
to delete the .idea/dictionaries directory, even though it does not
contain any .rej or .log files and is explicitly set to excluded

```

## Devste Devste, 2025-10-05 11:33

Subject: Re: git clean deletes negated pathspec if directory causing data loss
Message-ID: <CANM0SV3iRF=jVAFj=+rrgOC_nXEJ=_v1bUG88n3T9+PQD=a2-A@mail.gmail.com>
URL: https://gitlist.dev/e/CANM0SV3iRF%3DjVAFj%3D%2BrrgOC_nXEJ%3D_v1bUG88n3T9%2BPQD%3Da2-A%40mail.gmail.com
In-Reply-To: <CANM0SV0R7OYJKeBAs5-WY2Wqp7VxhKv5=LQ4cuDd6MaQwNqGNw@mail.gmail.com>

```
Just to clarify: it's not about the negation not working, but clean
ignores the pathspec for ignored directories completely. I only want
to delete .rej and .log files, but it will also delete gitignored
directories (that may or may not contain any .rej or .log files)

On Sun, 5 Oct 2025 at 13:27, Devste Devste <devstemail@gmail.com> wrote:
>
> I am using:
> git version 2.51.0.windows.1
>
> Run:
> echo .idea/dictionaries >> .gitignore
> mkdir -p .idea/dictionaries
> touch .idea/dictionaries/foo.xml
> git clean -f -f -d -X --dry-run -- ':!/.idea/dictionaries' ':*.rej' ':/*/*.log'
>
> Outputs:
> Would remove .idea/dictionaries/
>
> No matter how you specify the pathspec (':!.idea',...) it always wants
> to delete the .idea/dictionaries directory, even though it does not
> contain any .rej or .log files and is explicitly set to excluded

```

## Devste Devste, 2025-10-29 20:00

Subject: Re: git clean deletes negated pathspec if directory causing data loss
Message-ID: <CANM0SV3qgqjWeAu+NvfPxeDLgo6-eRWKmi9xkpUtN6w7LJh7oQ@mail.gmail.com>
URL: https://gitlist.dev/e/CANM0SV3qgqjWeAu%2BNvfPxeDLgo6-eRWKmi9xkpUtN6w7LJh7oQ%40mail.gmail.com
In-Reply-To: <CANM0SV3iRF=jVAFj=+rrgOC_nXEJ=_v1bUG88n3T9+PQD=a2-A@mail.gmail.com>

```
Anyone?

On Sun, 5 Oct 2025 at 13:33, Devste Devste <devstemail@gmail.com> wrote:
>
> Just to clarify: it's not about the negation not working, but clean
> ignores the pathspec for ignored directories completely. I only want
> to delete .rej and .log files, but it will also delete gitignored
> directories (that may or may not contain any .rej or .log files)
>
> On Sun, 5 Oct 2025 at 13:27, Devste Devste <devstemail@gmail.com> wrote:
> >
> > I am using:
> > git version 2.51.0.windows.1
> >
> > Run:
> > echo .idea/dictionaries >> .gitignore
> > mkdir -p .idea/dictionaries
> > touch .idea/dictionaries/foo.xml
> > git clean -f -f -d -X --dry-run -- ':!/.idea/dictionaries' ':*.rej' ':/*/*.log'
> >
> > Outputs:
> > Would remove .idea/dictionaries/
> >
> > No matter how you specify the pathspec (':!.idea',...) it always wants
> > to delete the .idea/dictionaries directory, even though it does not
> > contain any .rej or .log files and is explicitly set to excluded

```

## Johannes Schindelin, 2025-10-30 08:49

Subject: Re: git clean deletes negated pathspec if directory causing data loss
Message-ID: <a77f4e6f-86a3-ea15-feb5-4d08636ffd94@gmx.de>
URL: https://gitlist.dev/e/a77f4e6f-86a3-ea15-feb5-4d08636ffd94%40gmx.de
In-Reply-To: <CANM0SV3qgqjWeAu+NvfPxeDLgo6-eRWKmi9xkpUtN6w7LJh7oQ@mail.gmail.com>

```
Hi Devste,

On Wed, 29 Oct 2025, Devste Devste wrote:

> Anyone?
> 
> On Sun, 5 Oct 2025 at 13:33, Devste Devste <devstemail@gmail.com> wrote:
> >
> > Just to clarify: it's not about the negation not working, but clean
> > ignores the pathspec for ignored directories completely. I only want
> > to delete .rej and .log files, but it will also delete gitignored
> > directories (that may or may not contain any .rej or .log files)
> >
> > On Sun, 5 Oct 2025 at 13:27, Devste Devste <devstemail@gmail.com> wrote:
> > >
> > > I am using:
> > > git version 2.51.0.windows.1
> > >
> > > Run:
> > > echo .idea/dictionaries >> .gitignore
> > > mkdir -p .idea/dictionaries
> > > touch .idea/dictionaries/foo.xml
> > > git clean -f -f -d -X --dry-run -- ':!/.idea/dictionaries' ':*.rej' ':/*/*.log'
> > >
> > > Outputs:
> > > Would remove .idea/dictionaries/
> > >
> > > No matter how you specify the pathspec (':!.idea',...) it always wants
> > > to delete the .idea/dictionaries directory, even though it does not
> > > contain any .rej or .log files and is explicitly set to excluded

I can reproduce, both on Windows and on Linux. (Note that I prefer the
`:(exclude).idea/directories` form because it is more descriptive and it
also does not run afoul of Bash's special handling of the exclamation
point).

Unfortunately, the code in question is quite convoluted, and the intention
is also not quite clear. The main problem seems to be to agree on what
`-X` should mean in conjunction with `:(exclude)`.

One interpretation (which I assume is yours): When `-X` implicitly adds
items to be removed, `:(exclude)` should be able to remove them again.

The interpretation of the authors of the logic I see in the source code,
though, seem to treat the `-X` as a completely separate mechanism that
overrides whatever `:(exclude)` may be specified on the command-line.

Honestly, I am unsure how to resolve this, especially given that the
overall architecture of `dir.c` (which contains the business logic of
exclusions specified e.g. by `.gitignore) seems to have grown so
organically as to result in a complex, hard-to-reason-about state.

In case the Git maintainer (whose call it is, ultimately, on which side to
land regarding above-mentioned options) sides with your interpretation,
here is a patch to add a regression test demonstrating your use case:

-- snipsnap --
diff --git a/t/t7300-clean.sh b/t/t7300-clean.sh
index 6f16f389319..6bc7c42a572 100755
--- a/t/t7300-clean.sh
+++ b/t/t7300-clean.sh
@@ -362,6 +362,14 @@ test_expect_success 'git clean -d -X with ignored tracked directory' '
 
 '
 
+test_expect_failure 'git clean -d -X with :(exclude)' '
+	test_when_finished "rm -rf build" &&
+	mkdir -p build &&
+	touch build/lib.so &&
+	git clean -d -X -- ":(exclude)build" &&
+	test_path_is_file build/lib.sh
+'
+
 test_expect_success 'clean.requireForce defaults to true' '
 
 	git config --unset clean.requireForce &&

```

## Junio C Hamano, 2025-10-30 17:04

Subject: Re: git clean deletes negated pathspec if directory causing data loss
Message-ID: <xmqqms585nik.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqqms585nik.fsf%40gitster.g
In-Reply-To: <a77f4e6f-86a3-ea15-feb5-4d08636ffd94@gmx.de>

```
Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:

> In case the Git maintainer (whose call it is, ultimately, on which side to
> land regarding above-mentioned options) sides with your interpretation,
> here is a patch to add a regression test demonstrating your use case:

Thanks for an opinion.

As "git clean -X" is not really "my thing", not in the sense that I
did not write it (I didn't, except for code clean-ups) but in the
sense that I see no good use case for the option and I rarely use it
myself, as long as its behaviour can be "explained", like you just
did, I do not have preference myself, and no strong motivation to
advocate changing it, risking potential disruption on existing
users.

```
