Skip to content
vpfau edited this page Oct 24, 2018 · 1 revision

git-merge-base

Find two commits common ancestor

Git

$ git merge-base oneCommit secondCommit

LibGit2Sharp

public string GetMergeBase(string a, string b)
{
    using (var repo = new Repository("path/to/your/repo"))
    {
        var aCommit = repo.Lookup<Commit>(a);
        var bCommit = repo.Lookup<Commit>(b);
        if (aCommit == null || bCommit == null)
            return null;
        var baseCommit = repo.ObjectDatabase.FindMergeBase(aCommit, bCommit);
        return baseCommit != null ? baseCommit.Sha : null;
    }
}
Clone this wiki locally