# git clean --exclude broken?

4 messages from 2024-11-24 to 2024-12-10. Participants: Homyee King, brian m. carlson.
Thread: https://gitlist.dev/t/62532

## Homyee King, 2024-11-24 11:10

Subject: git clean --exclude broken?
Message-ID: <1A13EB2A-6075-4CA9-855E-B72ACDA67C0C@gmail.com>
URL: https://gitlist.dev/e/1A13EB2A-6075-4CA9-855E-B72ACDA67C0C%40gmail.com

```
13 years ago, Todd Rinaldo found that —exclude option is not works with any options, for example, `git clean -Xd -e=foo` to clean the files/folders respect the .gitignore rule except the foo, but in real world, git only respect the git ignore standard rule and not work with -e option.

Today, 13 years later, the problem still exist, so I wonder is this a bug or should we clarify the “correct” usage of exclude in documentation, because it’s really confusing.

The discussion can be viewed by this link: https://git.vger.kernel.narkive.com/ulppacPK/clean-exclude-broken

Looking forward to your reaply!
```

## brian m. carlson, 2024-11-24 18:12

Subject: Re: git clean --exclude broken?
Message-ID: <Z0Nsq3lQZKJZxd-y@tapette.crustytoothpaste.net>
URL: https://gitlist.dev/e/Z0Nsq3lQZKJZxd-y%40tapette.crustytoothpaste.net
In-Reply-To: <1A13EB2A-6075-4CA9-855E-B72ACDA67C0C@gmail.com>

```
On 2024-11-24 at 11:10:57, Homyee King wrote:
> 13 years ago, Todd Rinaldo found that —exclude option is not works
> with any options, for example, `git clean -Xd -e=foo` to clean the
> files/folders respect the .gitignore rule except the foo, but in real
> world, git only respect the git ignore standard rule and not work with
> -e option.
> 
> Today, 13 years later, the problem still exist, so I wonder is this a
> bug or should we clarify the “correct” usage of exclude in
> documentation, because it’s really confusing.

I don't think this is the right syntax.  For example, I did this in my
checkout of the Git project:

----
$ touch foo
$ git clean -n
Would remove foo
$ git clean -n -e foo
$ git clean -f -e foo
$ ls foo
foo
----

When you use a short option, you don't specify the equals sign.  So you
can write `-e foo`, `-efoo`, `--exclude foo`, or `--exclude=foo`.
However, `-e=foo` is equivalent to `-e =foo`, which ignores `=foo`.
-- 
brian m. carlson (they/them or he/him)
Toronto, Ontario, CA

```

## brian m. carlson, 2024-11-28 20:58

Subject: Re: git clean --exclude broken?
Message-ID: <Z0jZfltm0O3y9QoW@tapette.crustytoothpaste.net>
URL: https://gitlist.dev/e/Z0jZfltm0O3y9QoW%40tapette.crustytoothpaste.net
In-Reply-To: <CABAKogYaajvedff2ihsC5g+156L-Oe2N_XR3c+uyyfoovVQ5gg@mail.gmail.com>

```
On 2024-11-26 at 06:13:44, Homyee King wrote:
> Thanks for replying.
> It's a typo mistake, the correct command is `git clean -Xdn -e foo` but the
> problem still exists which is `--exclude` option not work with `-X`, is
> that design so or a bug? For example:
> 
> simple-project/ │ ├── foo/ │
> |---- bar/ ├── .gitignore # contains foo/ bar/ │ └── test.ts # untracked
> file
> 
> I only want to clear all gitignored files/folder but *`foo/` *and keep the
> untrakced files/folders, so here's the command
> $ git clean -Xdn -e foo
> $  ls
> $  Would remove foo/ bar/

I don't think there's a command for what you want to do here.  You
basically want to clear only ignored files, but exclude one pattern from
the ignore rules.  `-e` doesn't do that: it _adds_ to the ignore rules,
not subtracts from them.

So this is what I'd expect to see here with your file structure:

----
% git clean -Xdn -e test.ts
Would remove bar/
Would remove foo/
Would remove test.ts
----

since by using `-e`, we've added the additional files to be ignored, and
with `-X`, we've said to remove the files.
-- 
brian m. carlson (they/them or he/him)
Toronto, Ontario, CA

```

## brian m. carlson, 2024-12-10 02:23

Subject: Re: git clean --exclude broken?
Message-ID: <Z1emFS-L-f4caBJ1@tapette.crustytoothpaste.net>
URL: https://gitlist.dev/e/Z1emFS-L-f4caBJ1%40tapette.crustytoothpaste.net
In-Reply-To: <CABAKogZQ4bAh-KPE79q0W7iBBoRmxW26RK_6VSg=6Y-Sg68tig@mail.gmail.com>

```
On 2024-12-09 at 04:53:24, Homyee King wrote:
> But if you try *git clean -xdn -e foo*, the *foo *will be excluded and not
> what you said *"it _adds_ to the ignore rules".  *It's the confusing part
> of *`-e` *option, and I think it should always exclude something within the
> given pattern.
> If we follow what you said, it should be *--include,* what do you think?

The `--exclude` option means "exclude this (mark this as ignored)
additionally".  I agree it could be misinterpreted, but `--include`
could as well.  The manual page says this about `-e`:

  Use the given exclude pattern in addition to the standard ignore rules
  (see gitignore(5)).

And the text says this about `-x`:

  Don’t use the standard ignore rules (see gitignore(5)), but still use
  the ignore rules given with -e options from the command line. This
  allows removing all untracked files, including build products. This
  can be used (possibly in conjunction with git restore or git reset) to
  create a pristine working directory to test a clean build.

That means that when you say `git clean -xdn -e foo`, Git ignores only
`foo`, and says it will remove everything else that would normally be
ignored (which is no longer ignored, and thus untracked).  Remember that
`git clean` only removes untracked files and directories, and `-x` just
makes all files normally ignored untracked temporarily for the life of the
command.  Equivalent text exists in the description section of the
manual page.

I don't think this is confusing or that the text is inaccurate, but of
course if you want to submit a patch to improve the text, I'm sure we'd
be happy to take a look.
-- 
brian m. carlson (they/them or he/him)
Toronto, Ontario, CA

```
