Skip to content

Instantly share code, notes, and snippets.

@RadValentin
Last active July 27, 2024 06:38
Show Gist options
  • Save RadValentin/226550d8b90133cfc77519771a03f340 to your computer and use it in GitHub Desktop.
Save RadValentin/226550d8b90133cfc77519771a03f340 to your computer and use it in GitHub Desktop.
Solve `yarn.lock` or `package.json` conflicts without losing your mind

NOTE: This guide is ONLY for devs who don't want to edit their yarn.lock file by hand. If you don't care about that please carry on.


So you've pulled the latest master

git checkout master
git pull

And checked out your favorite branch but when you try to merge master into it...

git checkout my-awesome-feature
git merge master

Uh-oh! Both your fave branch and master have changed stuff in yarn.lock

CONFLICT (content): Merge conflict in package.json
CONFLICT (content): Merge conflict in yarn.lock

Nooooooooo

Sure you could probably fix the package.json conflict "by hand" but the lockfile should not be touched 💩

All sorts of naughty thoughts might be going through your head at this point including conflict fix branches, merge stategies or even god-forbid... a rebase⏰⬅️🚗💨

Does GIT support a type L phase discriminating amplifier

But fear not my troubled friend, there's no need to go that far, here's how you can fix this in 3 easy steps:

  1. Write down the exact package names and versions that your branch added/removed/upgraded (eg: left-pad@^1.1.3).
  2. Checkout and stage package.json and yarn.lock from master.
git checkout --theirs package.json
git checkout --theirs yarn.lock
git add package.json yarn.lock
  1. Re-apply the packages you wrote down in step 1 (eg: yarn add left-pad@^1.1.3).

Now the conflicts should be all gone and you're free to go on your way (commit the merge or fix other unrelated conflicts).

swag

@ronald9715
Copy link

it works for me just doing the step 2

@AbdullahAnsarii
Copy link

Thanks man, you saved me.

@gajus
Copy link

gajus commented Jan 13, 2023

Just run yarn install. Yarn has a built-in mechanism to fix conflicts if it detects them present in the lock file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment