How to revert to previous packages if Laravel’s “composer update” breaks site?

When you run composer update and some packages break after the update, you can revert your project to the previous working state. Here’s how to handle it:

1. Check composer.lock file

After running composer update, the composer.lock file gets updated with the new versions of your dependencies. If things break, you can use Git to revert to the previous composer.lock.

2. Revert composer.lock using Git

If you are using Git and you committed the composer.lock file before running the update, you can easily go back to the previous version of the file. Here’s how:

git checkout HEAD^ composer.lock

This command checks out the previous version of the composer.lock file (the version before the last commit). If you want to revert to a specific commit, use the commit hash instead:

git checkout <commit-hash> composer.lock

3. Reinstall the previous dependencies

After reverting the composer.lock file, you need to install the previous versions of the packages defined in the restored composer.lock:

composer install

Note: Do not use composer update, as this will update the packages again. composer install will install the versions defined in composer.lock.

4. Commit the reverted composer.lock (if necessary)

After confirming that everything works as expected, commit the reverted composer.lock file:

git add composer.lock
git commit -m "Revert composer.lock to previous working state"

5. Lock Specific Package Versions (Optional)

If you want to prevent certain packages from being updated in the future, you can specify the exact versions in composer.json by using the caret (^) or tilde (~) version constraints. For example:

{
   "require": {
       "package/name": "^1.0"
   }
}

This ensures that composer update won’t update beyond the specified version.

By following these steps, you can safely revert your Composer dependencies back to a working state after an update causes issues.