Git: how to find shortest path between commits

723 Views Asked by At

Given a large git repository with hundreds of branches / thousands of commits, and an irregular branch merging strategy, how does one determine the shortest path between two commits? In other words, What path of branches from earlier commit A to later commit B crosses the least number of commits?

(In my case I'm trying to trace the history of a 'develop' branch that had inadvertently been merged into feature branches via fast-forward. A "shortest path" strategy seemed like a practical approach, but I've been looking through the help pages for git-log and git-rev-list [v1.8.3.1], but haven't found any option that seems to do this. Am I missing something obvious?)

2

There are 2 best solutions below

0
On

Maybe not fully related, but this worked for me "to trace the history of a 'develop'" so that I got log with only commits on 'develop' (without commits on merged branches).

git log 25ab137285..2fbdf0a100 --ancestry-path --first-parent --oneline

25ab137285 and 2fbdf0a100 are commits on desired branch (e.g. develop)

--first-parent - main part

--ancestry-path - believe that it helps

5
On

I'm not sure what you're asking, but to find how A got to B you can use this:

git log commitA..commitB

You can use the --graph and --simplify-merges flags to make it more clear.

You can also prettify the log like so:

git log --simplify-merges --graph --pretty="tformat:%C(yellow)%h%Creset\\ %Cgreen(%ar)%Creset\\ %C(blue)<%an>%Creset\\ %C(red)%d%Creset\\ %s"

and get output like this:

enter image description here