How to find the oldest common descendant of two commits in git?
Asked Answered
B

2

8

Given two (or more) commits, I want to find the oldest merge which joins them. Something like the opposite of git merge-base (which finds the youngest common ancestors). Notice that the commit I’m looking for will be a younger descendant of the starting commits.

In other words: I want to investigate the merge commit which merged two given commits (where changes to the same file happened).

Baldric answered 18/9, 2015 at 10:38 Comment(2)
Isn't it what you want? #8475948Runofthemine
@Runofthemine No, that question has a branch given, where the merge should be on. (I don’t know yet which branch has the oldest merge commit I’m looking for.)Baldric
R
1
oldest-merge() { 
        ( for c; do git log --all --merges --ancestry-path ^$c --pretty='%aI %h %s'; done ) \
        | sort | uniq -c | awk "\$1 == $# {print;exit}"
}

oldest-merge `git rev-list --all --grep=BUG-27182`  # e.g. 

the final awk takes the first commit that showed up in all the merge-ancestry lists.

Respire answered 15/10, 2019 at 21:11 Comment(1)
The output is given in reverse chronological order by default.Bookcase
P
0

If $a and $b are the two commit hashes and they are both contained in master, then say

git rev-list $a..master --ancestry-path >a
git rev-list $b..master --ancestry-path >b

Then use a diff tool to find the last common line of a an b.

Pommard answered 1/11, 2018 at 17:11 Comment(1)
I've tried this and when using cmp for comparison, I do find a common commit. However this is not the "earliest"/"oldest" common commit that has both a and b commit's changes. It's not even the "latest"/"youngest" commit either. It's somewhere in between. Not entirely sure.Cycle

© 2022 - 2024 — McMap. All rights reserved.