# [feature] git clean ignored files in nested repositories support

2 messages from 2025-07-26 to 2025-08-01. Participants: Alon Bar-Lev.
Thread: https://gitlist.dev/t/63854

## Alon Bar-Lev, 2025-07-26 12:00

Subject: [feature] git clean ignored files in nested repositories support
Message-ID: <CAOazyz08vf65ezNMuUyOCYYHBGmDSeCtzXn=WdZAWGO9ZjCkAQ@mail.gmail.com>
URL: https://gitlist.dev/e/CAOazyz08vf65ezNMuUyOCYYHBGmDSeCtzXn%3DWdZAWGO9ZjCkAQ%40mail.gmail.com

```
Hi,

I am sure this was discussed in the past, I could not find any
explicit discussion.

The `git clean` is a handy command, it can clean directories,
including ignore files and also handle nested git repositories if a
second -f is given.

    git clean -dxff

However, as far as I understand, it is not possible to clean ignored
files in nested repositories, the `x` is applied only to the local
repository.

As workaround following command may be used:

    git clean -dxff && git submodule foreach git clean -dxff

However, I expect the double `f` to take into account the `x` and also
clean ignored files from the nested repository.

I am unsure if this is a bug or intentional.

If this is intentional, maybe a patch to have a second -x would be
acceptable? so that we could have something like:

   git clean -dxxff

Any other suggestions? It would be nice to be able to clean the entire
tree with a simple git clean command consistently in all scenarios.

Regards,
Alon

```

## Alon Bar-Lev, 2025-08-01 19:00

Subject: Re: [feature] git clean ignored files in nested repositories support
Message-ID: <CAOazyz32EAhqXmSTG7BVW2gLkgJort5ghR4+e2PjSn_b5pcBYg@mail.gmail.com>
URL: https://gitlist.dev/e/CAOazyz32EAhqXmSTG7BVW2gLkgJort5ghR4%2Be2PjSn_b5pcBYg%40mail.gmail.com
In-Reply-To: <CAOazyz08vf65ezNMuUyOCYYHBGmDSeCtzXn=WdZAWGO9ZjCkAQ@mail.gmail.com>

```
Hello,

I tried to trace the code and it seems like there is a bug or
different behavior than the documented behavior, the recursive
implementation in `dir.h` is quite complex, this is not a trivial fix.

"""
man git-clean
       -f, --force
           If the Git configuration variable clean.requireForce is not
set to false, git clean will refuse to delete files or directories
unless given -f or -i. Git will refuse to
           modify untracked nested git repositories (directories with
a .git subdirectory) unless a second -f is given.
"""

I expect untracked files to be removed from the nested git directory
when `git clean -dxff` is executed, previously I thought that the
ignored files were the issue.

Reproduction:

ˋˋˋ
mkdir test
cd test
git init .
git commit -a -m "root" --allow-empty
git submodule add https://github.com/git/htmldocs module1
git commit -a -m "init"
touch module1/junk.txt
git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
  (commit or discard the untracked or modified content in submodules)
modified:   module1 (untracked content)

no changes added to commit (use "git add" and/or "git commit -a")
ˋˋˋ

Now, clean recursive and allow nested (double `-f`):

ˋˋˋ
git clean -dxff
git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
  (commit or discard the untracked or modified content in submodules)
modified:   module1 (untracked content)

no changes added to commit (use "git add" and/or "git commit -a")
ˋˋˋ

Expected behavior: the `module1/junk.txt` removed.
Actual behavior: the `module1/junk.txt` remains.

For some reason I remember that a few years ago this worked as
expected, at least in removing untracked files recursively and in
nested repositories.

Thanks,
Alon


On Sat, 26 Jul 2025 at 15:00, Alon Bar-Lev <alon.barlev@gmail.com> wrote:
>
> Hi,
>
> I am sure this was discussed in the past, I could not find any
> explicit discussion.
>
> The `git clean` is a handy command, it can clean directories,
> including ignore files and also handle nested git repositories if a
> second -f is given.
>
>     git clean -dxff
>
> However, as far as I understand, it is not possible to clean ignored
> files in nested repositories, the `x` is applied only to the local
> repository.
>
> As workaround following command may be used:
>
>     git clean -dxff && git submodule foreach git clean -dxff
>
> However, I expect the double `f` to take into account the `x` and also
> clean ignored files from the nested repository.
>
> I am unsure if this is a bug or intentional.
>
> If this is intentional, maybe a patch to have a second -x would be
> acceptable? so that we could have something like:
>
>    git clean -dxxff
>
> Any other suggestions? It would be nice to be able to clean the entire
> tree with a simple git clean command consistently in all scenarios.
>
> Regards,
> Alon

```
