Is there a way to identify reverted commits when running git rev-list B2 ^B1?

69 Views Asked by At

I'm trying to find all the commits that exist in one branch, but don't exist in another. Taking the image below, I want to know what's in B2 that isn't in B1. When I try the following command:

git rev-list B2 ^B1

I only get back commit c2, even though c1 was reverted before going into B1. Is there a way to detect this situation, so as to get back both c1 and c2 since their changes aren't technically contained in B1?

Comparing two branches with reverted commits in between

1

There are 1 best solutions below

0
On

Making some assumptions about your diagram—specifically, that there is a merge commit at the point where the diagonal line joins the top line—commit c1 literally is in branch B1. Operations like B2 ^B1 use the commit graph—the DAG made up of commits and their backwards-looking linkages—to find commits. They completely ignore the snapshots in these commits; they are just walking the commit graph.

It's true that besides C1, there is another commit, also contained in branch B1, that in effect un-does C1. You can't find that easily as Git does not store that information on its own. You can of course put that information into, e.g., the commit messages, and write your own code to traverse the list of commits contained within B1 to see if there are commits that have such log messages, but that's something you have to do, not something Git will do on its own.

(It seems to me that this might be an XY problem. That is, maybe you don't want a list of commits but rather a comparison of the tip commit snapshots of branches B1 and B2, perhaps with some filtering applied.)