8 – 'composer require“ failed because "docroot / core source directory contains uncommitted changes"

Symptom: You have a script that fails when running from one composer need command, generating the following results:

$ ./example.sh
Gathering fixes for the root package.
Removed the drupal / core package so that it can be reinstalled and fixed.
- Remove drupal / core (8.6.x-dev)

In VcsDownloader.php, line 242:

The docroot / core source directory contains uncommitted changes.

Attempt to execute the offense composer need command manually gives as result the following output:

$ composer need drupal / examples
Gathering fixes for the root package.
Removed the drupal / core package so that it can be reinstalled and fixed.
- Remove drupal / core (8.6.x-dev)
The package contains modified files:
M core.services.yml
M includes / install.core.inc
M includes / install.inc
M lib / Drupal / Core / Config / ConfigInstaller.php
Library / Drupal / Core / Config / ExtensionInstallStorage.php
Library / Drupal / Core / Config / InstallStorage.php
M lib / Drupal / Core / Entity / EntityViewBuilder.php
M lib / Drupal / Core / EventSubscriber / ConfigImportSubscriber.php
M lib / Drupal / Core / Extension / ExtensionDiscovery.php
M lib / Drupal / Core / Extension / ModuleExtensionList.php
19 other modified files, choose "v" to display the complete list
Discard changes [y,n,v,d,?]?

Conditions: You use Composer to manage the dependencies of your Drupal site and apply patches with cweagans / composer-patches. You may be using Acquia BLT.