Efficiently resolving composer.lock merge conflicts

Search for a command to run...

No comments yet. Be the first to comment.
Flag emojis are a fun and visual way to represent countries and regions. These emojis are part of the Unicode standard and are created using a pair of regional indicator symbols. In this article, we will explore how to convert 2-letter ISO 3166-1 cou...

If you’re like many developers, you know the frustration of being stuck on an older PHP version. Upgrading to the latest and greatest can be a daunting task, especially when you're dealing with legacy code and tight deadlines 🤬. But fear not! Symfon...

Have you ever wished some queued jobs would be processed way ahead others? For example, if you want your VIP client's transanctions to be completed before everyone else's, this article might help you. Simple approach ✨ If you have only a few tiers of...

As a part of your digital identity, you may want to use a custom domain for your personal email address. In this article I'll show you how to easily set up email receiving and sending without breaking a bank. Why do I need a custom email domain in th...

When reviewing a Pull Request that touched composer.json and composer.lock files you might notice that the diffs are usually pretty huge and it's almost impossible to tell what exactly happened there - which packages got added, updated, downgraded or...

When working in a team on a PHP project using Composer, you have probably encountered a problem when multiple people added, removed or updated some packages in composer.json and composer.lock on main branch and GIT welcomed you with this message in the morning:
Auto-merging composer.lock
CONFLICT (content): Merge conflict in composer.lock
Resolving such conflict is not a straightforward operation due to the structure of the file which contains all locked dependencies information and a content hash.
In this article I will show you few methods to resolve such conflict and make your life a bit easier.
git checkout --theirs composer.lock
composer update php # the "php" is important here
I’ve seen people doing git checkout --ours composer.lock and pushing the changes… Since it’s overwriting all others’ changes with yours, it’s basically an equivalent of force-pushing changes to main branch — never do that!
Another bad (but not as destructive) idea would be to remove composer.lock file and recreate it using composer update. This might seem like a good idea if you don’t really care about your locked versions and trust your version constraints and tests 100%. In real life, this will most likely introduce some unexpected changes into your feature branch and unnecessarily increase the diff.
This method is suitable for most common scenarios, when some packages were added or updated in main branch and you are trying to merge it into your feature branch with some other packages added. Assuming there are no dependency conflicts, you are good to go with this solution after merging the composer.json file.
First, accept the changes from base (main) branch:
git checkout --theirs composer.lock
Then, trigger an update of the packages you added or removed:
composer update php
Please note that you may pass any installed (or even not installed) package name to the update command for it to work but passing php will only touch the packages you just added or removed, without unnecessarily updating any extra packages to keep your changes clean.

composer update) from your feature branch will be gone — you should re-run the update afterwards if neededThis method is described in the official Composer documentation. In some cases, when there might be some conflicting packages, it might be better to redo you package additions/removals on top of most recent changes from main branch — similarly as applying changes during a rebase.
First, accept the changes from base (main) branch:
git checkout --theirs composer.lock composer.json
Then, add all the packages you added or removed in your feature branch:
composer require package/a:^1.2
composer require package/b:^2.0
composer remove package/c
...
When you’re done, make sure your composer.json and composer.lock files are valid using composer validate command.
composer update) from your feature branch will be gone — you should re-run the update afterwards if neededSince above methods will not preserve the locked versions on packages that have been added on your feature branch, you may use an external tool such as Composer Git Merge Driver that automates this process, keeping your locked versions and minimizing the diff produced. Please refer to the official repository for installation and usage instructions.

Please keep in mind that using Method 1 or 2 will discard the information about the exact package version added in your feature branch. This is because composer will lock the newest available version matching your constraints at the time of merge. If your package was constrained as ^1.0 and locked at 1.1.0 , it might be updated to 1.2.0 while performing these steps. Most of the time it should not produce any side effects, especially if your version constraints are valid.