Aha. In this case, I do the following:
1. Clone the whole Cube library package (or only some middlewares) into a separate directory, and add a branch in the version control.
2. Modify the Eclipse project so that it uses links into that cloned package instead of subdirectories under the project directory. In the Cube[MX] .ioc project change option "copy library files" to "add references", and change the ST library path to your new clone. Change also all the include paths and so on.
Then re-generation won't touch your changes in the cloned directory, and it can be shared among several projects. Some of projects can use the original library and others can use the clone, or other clones.
But this is much less simple as the previous recipe. Using Eclipse "build variables" makes this a bit easier.
Another way is to delete Drivers and Middlewares directories from the project and replace them with symlinks to the cloned & patched libraries (symlinks work great in Windows, yep) - but this will confuse CubeMX re-generation: it will copy original lib. files to your clone so you again will have to recover your changes.