threads / discuss / 65175

Option for "git submodule foreach" to also run on the parent git?

Subject: Option for "git submodule foreach" to also run on the parent git?

## tl;dr

6 messages between Mar 9, 2026 and Mar 9, 2026.

replies: 5people: 5as markdown or json

Florian Schmidt· Mar 9, 2026, 12:52 UTC · lore
Hey everyone,

would there be an appetite for a new option to "git submodule foreach" to run the foreach command on the parent git repo as well?

I occasionally use "git submodule foreach", for example, to make dev branches on all the submodules if I expect to touch them all during a dev cycle. Even if I don't need them on all submodule, once I'm done with everything, I might run something like "git submodule foreach 'git branch -d <foo> || :'" for easy cleanup.

In either case, I likely also want to run the same command on the parent git. That's easily achieved by just running it manually one more time, and I'm sure it's not hard to make a git alias for that either. But I wonder whether the workflow is common enough that there's interest in adding a command-line option for that? I'd be happy to have a look in that case.

Also, tangentially related, "|| :" works, but what about a command line option to continue on non-zero return values and not abort?

Cheers, Florian

Junio C Hamano· Mar 9, 2026, 15:15 UTC · re: Florian Schmidt · lore

Re: Option for "git submodule foreach" to also run on the parent git?

Florian Schmidt <flosch@nutanix.com> writes:
> would there be an appetite for a new option to "git submodule foreach" 
> to run the foreach command on the parent git repo as well?

Not from me. After all, the top-level superproject is not a submodule from the point of view of the tree, is it? If it were "git submodule-and-superproject foreach", then perhaps, but at that point, wouldn't it be simpler to explain what you are doing as

    do-this . ;# do it here
    git submodule foreach do-this ;# do it there all over
instead?
Florian Schmidt· Mar 9, 2026, 17:43 UTC · re: Junio C Hamano · lore

Re: Option for "git submodule foreach" to also run on the parent git?

On 2026-03-09 15:15, Junio C Hamano wrote:
Show 8 quoted lines
> Florian Schmidt <flosch@nutanix.com> writes:
> 
>> would there be an appetite for a new option to "git submodule foreach"
>> to run the foreach command on the parent git repo as well?
> 
> Not from me.  After all, the top-level superproject is not a
> submodule from the point of view of the tree, is it?  If it were
> "git submodule-and-superproject foreach", then perhaps,

True, I was thinking that, but figured an option such as "--also-parent" would be sufficiently clear as to what's happening. But I can see that it's maybe pretty niche and potentially confusing.

Show 5 quoted lines
> but at that
> point, wouldn't it be simpler to explain what you are doing as
> 
>      do-this . ;# do it here
>      git submodule foreach do-this ;# do it there all over

That's basically what's I'm doing. I figured instead of doing "foo && git submodule foreach foo", a "git submodule foreach --also-parent foo" would be marginally neater. But I don't feel strongly about it.

Cheers, Florian

Jeff King· Mar 9, 2026, 19:30 UTC · re: Florian Schmidt · lore

Re: Option for "git submodule foreach" to also run on the parent git?

On Mon, Mar 09, 2026 at 05:43:14PM +0000, Florian Schmidt wrote:
Show 9 quoted lines
> > but at that
> > point, wouldn't it be simpler to explain what you are doing as
> > 
> >      do-this . ;# do it here
> >      git submodule foreach do-this ;# do it there all over
> 
> That's basically what's I'm doing. I figured instead of doing "foo && git
> submodule foreach foo", a "git submodule foreach --also-parent foo" would be
> marginally neater. But I don't feel strongly about it.

These days we also have git-for-each-repo. It is primarily about running a command in a set of otherwise unrelated repos. But I wonder if:

  git for-each-repo --submodules do-this

would be a natural fit as an alternative to "git submodule foreach"? And then you can imagine a "--current" option to discover the current repo and include it in the list. And then:

  git for-each-repo --submodules --current do-this
would do what you want.

I dunno. I do not have any need for this myself, but just thinking out loud.

-Peff
brian m. carlson· Mar 9, 2026, 21:03 UTC · re: Jeff King · lore

Re: Option for "git submodule foreach" to also run on the parent git?

On 2026-03-09 at 19:30:01, Jeff King wrote:
Show 15 quoted lines
> These days we also have git-for-each-repo. It is primarily about running
> a command in a set of otherwise unrelated repos. But I wonder if:
> 
>   git for-each-repo --submodules do-this
> 
> would be a natural fit as an alternative to "git submodule foreach"? And
> then you can imagine a "--current" option to discover the current repo
> and include it in the list. And then:
> 
>   git for-each-repo --submodules --current do-this
> 
> would do what you want.
> 
> I dunno. I do not have any need for this myself, but just thinking out
> loud.

I think this would be quite useful especially if it could operate on submodules before the parent repository. My particular use case is with the SHA-256/SHA-1 interoperability work, since that would allow people to recursively convert submodules into interoperability mode and then the main repository (which needs the submodule mappings first). This seems like a thing others will want to do as well.

So while I'm not planning to work on this myself, I think this would be a fantastic feature to add.

-- 
brian m. carlson (they/them)
Toronto, Ontario, CA
D. Ben Knoble· Mar 9, 2026, 22:14 UTC · re: Florian Schmidt · lore

Re: Option for "git submodule foreach" to also run on the parent git?

On Mon, Mar 9, 2026 at 8:56 AM Florian Schmidt <flosch@nutanix.com> wrote:
Show 24 quoted lines
>
> Hey everyone,
>
> would there be an appetite for a new option to "git submodule foreach"
> to run the foreach command on the parent git repo as well?
>
> I occasionally use "git submodule foreach", for example, to make dev
> branches on all the submodules if I expect to touch them all during a
> dev cycle. Even if I don't need them on all submodule, once I'm done
> with everything, I might run something like "git submodule foreach 'git
> branch -d <foo> || :'" for easy cleanup.
>
> In either case, I likely also want to run the same command on the parent
> git. That's easily achieved by just running it manually one more time,
> and I'm sure it's not hard to make a git alias for that either. But I
> wonder whether the workflow is common enough that there's interest in
> adding a command-line option for that? I'd be happy to have a look in
> that case.
>
> Also, tangentially related, "|| :" works, but what about a command line
> option to continue on non-zero return values and not abort?
>
> Cheers,
> Florian

This is not particularly general-purpose, but don't we have submodule.recurse (-> switch, checkout) and submodule.propagateBranches to make this kind of branching workflow easier?

-- 
D. Ben Knoble

← back to recent threads