Skip to content
This repository was archived by the owner on Feb 3, 2023. It is now read-only.

Git merge base

Viktor Nemes edited this page Feb 4, 2015 · 3 revisions

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 = Repository.Lookup<Commit>(a);
        var bCommit = Repository.Lookup<Commit>(b);
        if (aCommit == null || bCommit == null)
            return null;
        var baseCommit = Repository.Commits.FindMergeBase(aCommit, bCommit);
        return baseCommit != null ? baseCommit.Sha : null;
    }
}
Clone this wiki locally