Skip to content

Instantly share code, notes, and snippets.

View omaralamoudi's full-sized avatar

Omar Alamoudi omaralamoudi

View GitHub Profile
@omaralamoudi
omaralamoudi / git-checkout-or-create-new-branch-recursively-in-repo-and-submodules.md
Last active May 25, 2022 15:40
Git checkout or create new branch recursively in repo and submodules

Git checkout or create new branch recursively in repo and submodules

If you are using submodules in a git repo, you might find yourself sometimes wanting to execute a command recursively in the primary/parent repo as well as all submoduls. Well, I found myself in that position, and I'm writing this for my future self.

What if you want to perform a task repeatedly in all submodules?

The most basic approach to this is to use git submodule foreach <command>, where <command> can be any git command, or a shell command including commands such as git status . or git checkout main This basically loops through all submodules and performs the command specified, but it does not execute this command in the root repo.

What if your submodules have their own submodules?

Well, that is luckely easy too. Simply add the keywoard --recursive as following: git submodule foreach --recursive