2020-08-31 When were changes from a specific commit merged

A situation that happens to me from time to time is when I have a particular commit in my Git repository (often taken from the output of git blame) and I’d like to know when (and with what pull request) it was merged to the main branch (usually develop).

This is not as easy as it sounds. I could take this commit and find the first merge among its descendants, but this would fail in more complicated scenarios (like develop being merged to the feature branch, which happens pretty often).

It turns out that there is a quite old question about this particular use-case on StackOverflow. There are a few answers to that question, some older, some newer, but the consensus seems to be that what needs to be done is more or less this. First, take the result of

git rev-list some_commit..develop --ancestry-path

This shows all commits that are descendants of some_commit and ancestors of develop – in other words, everything that happened “between” some_commit and develop (where “between” is not in the chronological sense, but in the sense of the position on the history graph). Then, run

git rev-list some_commit..develop --first-parent

This shows all ancestors of develop from the main development line. Now, the last entry present in both lists (git rev-list sorts in reverse chronological order) is the exact place where both sets first crossed, that is, the moment some_commit found its way to develop.

This is pretty much it. The only thing left is to package this knowledge into some nice script, and this is precisely what a few people did (several such scripts are mentioned in the answers to the aforementioned question). I tried the one here (though I only skimmed through the code) and it seems to work very well. (I put the script into ~/bin and called it git-find-merge, so I effectively created for myself a new Git command git find-merge.)

CategoryEnglish, CategoryBlog, CategoryGit