[Guide] Dealing with annoying submodule changes when switching branch

About

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?

Solution

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.

Satisfied?

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

2 Likes

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