WB
Home About Notes

Comparing Git Branches

  20th August 2024
  1 minute
  git

When I have some git branches left unmerged it can be useful to compare what commits are on one branch and not the other.

To check if a branch has been merged into another branch (e.g., main), you can use the following Git command:

git branch --merged production
* production
  staging

This command lists all branches that have been merged into the main branch. If your branch appears in the list, it has been merged.

Alternatively, you can use the following command to check if a specific branch (e.g., feature-branch) has been merged into main:

git merge-base --is-ancestor feature-branch main && echo "Merged" || echo "Not merged"

You can list the commits that are in a branch but not yet merged into another branch (e.g., main) using the following Git command:

git log production..2fa
edscreening git:2fa ❯ git log production..2fa

commit f9256b8129c3b498b999c4c3311643539b373be1 (HEAD -> 2fa, origin/2fa)
Merge: 89ec6c5f 7ac72594
Author: Will <[email protected]>
Date:   Fri Aug 16 15:13:58 2024 +0100

    Merge branch 'production' into 2fa

commit 89ec6c5ffb4febc94c18270b7429b429e97dbefc
Author: Will <[email protected]>
Date:   Wed Jul 10 14:04:19 2024 +0100

    notes

commit 1bcfb39f118a80304a751482081bec35a3216ba3
Author: Will <[email protected]>
Date:   Thu Jun 27 09:46:40 2024 +0100

    start 2fa

This command will list the commits in feature-branch that are not in main, prefixed with a + if the commit is unique to feature-branch and a - if it is already in main.

git cherry -v main feature-branch

You can of course get similar comparisons on github

bucket

Clicking through, you get an analysis and a diff.

bucket

Lets make something together!

Get in touch at [email protected]


Copyright 2025 © All rights Reserved.
Crafted Web Development. Built With