BERLIN—Germany’s industrial heavyweights are teaming up to retrain workers in areas such as software and logistics to fill a growing skills gap and avoid layoffs among workers of all ages as the economy shifts to clean energy and online shopping. More than 36 major companies, ranging from auto suppliers such as Continental and Bosch to…