threads / discuss / 63854

[feature] git clean ignored files in nested repositories support

Subject: [feature] git clean ignored files in nested repositories support

## tl;dr

2 messages between Jul 26, 2025 and Aug 1, 2025.

replies: 1people: 1as markdown or json

Alon Bar-Lev· Jul 26, 2025, 12:00 UTC · lore
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· Aug 1, 2025, 19:00 UTC · re: Alon Bar-Lev · lore

Re: [feature] git clean ignored files in nested repositories support

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:
Show 35 quoted lines
>
> 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

← back to recent threads