[Guide] Dealing with annoying submodule changes when switching branch


As PX4-Autopilot has ~18 submodules (and even more, if you count the submodules inside the submodule), quite often when you switch branches you will encounter errors like this:

And as shown above, removing each of the “Untracked files” (which are the submodules that used to exist in the branch you switched from, but don’t exist anymore in the new branch) is quite tedious and annoying! What can we do about this?


Well you may first think that git clean -fd (Remove untracked files forcefully, including directories) will do the job, but that’s not the case. So what is the solution?

In fact, this is a common frustration/problem for projects with large number of submodules.

Special script to parse out Untracked files

First solution is to

Special script to de-initialize and re-initialize submodules

In the end, this ‘untracked submodule’ problem occurs because each branch has its own set of submodules defined and this changes.


TODO- Finish the article, as I am not sure yet how to solve it completely neither :smirk:


Try this in the base px4 firmware folder:

git clean -xfdf
git submodule foreach --recursive git clean -xfdf
git reset --hard --recurse-submodules
git submodule foreach --recursive git reset --hard
git submodule update --init --recursive

source: Cleans and resets a git repo and its submodules · GitHub