Currently Available: Need a skilled Software Developer for your next project?
Categories
Laravel PHP

Laravel/PHP: Remove vendor/ AND composer.lock When Switching Branches

You're trying to switch branches and git yells at you:

git checkout feature-branch
error: Your local changes to the following files would be overwritten by checkout:
        composer.lock
Please commit your changes or stash them before you switch branches.
Aborting

Standard PHP Projects

Here's how to fix it:

# First, clean up your current state
rm -rf vendor/
rm composer.lock
# Now you can switch branches
git checkout feature-branch
# And finally get your dependencies
composer install

This works because you're removing the conflicting files before trying to switch branches. The vendor directory and lock file from your current branch are gone, so git can freely switch to the other branch. Then Composer will set up everything fresh based on that branch's composer.json.

Save yourself some typing with this alias in your .bash_aliases or .zshrc:

alias fresh-composer='rm -rf vendor/ && rm composer.lock && composer install'

Then whenever you need to switch branches with different dependencies:

rm -rf vendor/ && rm composer.lock  # Clear the way first
git checkout other-branch
fresh-composer

Laravel Sail Projects

If you're using Laravel Sail, there's a catch: Sail lives in your vendor directory! When you delete vendor/, you can't use sail composer install anymore because Sail itself is gone. Here's how to handle it:

# Delete current state
rm -rf vendor/
rm composer.lock
# Get the lock file from the branch you're switching to
git checkout feature-branch composer.lock
# Run composer install via Docker directly
docker run --rm \
    -u "$(id -u):$(id -g)" \
    -v "$(pwd):/var/www/html" \
    -w /var/www/html \
    laravelsail/php83-composer:latest \
    composer install --ignore-platform-reqs

Replace php83 in laravelsail/php83-composer:latest with your PHP version:

  • PHP 7.4: laravelsail/php74-composer:latest
  • PHP 8.0: laravelsail/php80-composer:latest
  • PHP 8.1: laravelsail/php81-composer:latest
  • PHP 8.2: laravelsail/php82-composer:latest
  • PHP 8.3: laravelsail/php83-composer:latest

After this runs, Sail will be back in your vendor directory and you can use it normally again.

For Sail projects, you might want a more specific alias:

alias fresh-sail-composer='rm -rf vendor/ && rm composer.lock && docker run --rm -u "$(id -u):$(id -g)" -v "$(pwd):/var/www/html" -w /var/www/html laravelsail/php83-composer:latest composer install --ignore-platform-reqs'

Now you can switch branches cleanly regardless of whether you're using standard PHP or Laravel Sail:

rm -rf vendor/ && rm composer.lock  # Clear the way first
git checkout other-branch
# For standard PHP projects:
fresh-composer
# For Sail projects:
fresh-sail-composer
What I'm building

Delegate tasks. Get software.

Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.

Take a look at vroni.com

Subscribe to my newsletter

Get new posts when I publish them.

I respect your privacy. Unsubscribe at any time.

Leave a Reply

Your email address will not be published. Required fields are marked *