• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (deprecated)
You are here: Home / Dev Tips / Compare composer.json on Two Different Git Branches

Compare composer.json on Two Different Git Branches

Last updated on October 12, 2023 by Sal Ferrarello

I have composer.json files on two different branches and I want to compare their (non-dev) dependencies. This is the command I run to compare the require section of composer.json on my current branch to the require section of composer.json on the branch env_qa.

diff <(jq --sort-keys '.require' composer.json) <(git show env_qa:composer.json | jq --sort-keys '.require')

I’m using jq to extract the dependencies under the require key in composer.json.

Alias

Adding the following function to my .zshrc configuration

function jqgcdiff() {
  diff <(jq --sort-keys '.require' composer.json) <(git show "$1":composer.json | jq --sort-keys '.require')
}

allows me to compare the .require section of composer.json on the current branch to the branch I provide as an argument to the function.

e.g.

jqgcdiff main

will compare the .require section of the composer.json file on the current Git branch to the .require section of the composer.json file on the main branch.

The name comes from jq, g for Git, c for Composer, and diff: jqgcdiff.

Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP and JavaScript developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a principal engineer.

Filed Under: Dev Tips, Solution Tagged With: composer, Git, jq, jq composer

Reader Interactions

Comments

  1. Chrispian Burks says

    October 26, 2022 at 9:39 am

    Excellent tip! This is gonna come in super useful. But even more so, learning about jq for the first time. Adding to my toolbox.

    Reply

Leave a Reply Cancel reply

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

Copyright © 2026 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in