How is a merge-base done in JGit?

I’m looking at the JGit Documentation, currently at version, and I cannot find the equivalent of git-merge-base.

I would like to determine if a branch is up-to-date, behind, ahead, or diverged, as shown in git: check if pull needed.

  • How to force pull before push
  • Git show whole file changes
  • git doesn't work behind proxy since version 1.7.9
  • Why isn’t eol=crlf honored in .gitattributes
  • How would I configure TeamCity to support a GitHub pull-request model for source-code integrations?
  • What are those 'WIP' and 'index' commits that appear after stashing?
  • What is the most concise way in JGit to find as good common ancestors as possible for a merge?

  • Limit Git Diff to one or more functions?
  • Git aliases - command line autocompletion of branch names
  • Composer - using a local repository
  • Merge two git repository histories
  • “No commit specified and merge.defaultToUpstream not set”
  • How to only push a specific tag to remote?
  • 3 Solutions collect form web for “How is a merge-base done in JGit?”

    You can use RevFilter.MERGE_BASE for that:

    RevWalk walk = new RevWalk(repository);
    RevCommit mergeBase =;

    Also note that there is BranchTrackingStatus if all you are interested in is ahead/behind count of a branch compared to its remote-tracking branch.

    Here it is with JGit using BranchTrackingStatus:

    public enum TrackingStatus {
    public TrackingStatus getTrackingStatus() throws IOException, GitAPIException {
        Repository userRepo = new FileRepository(<path_to_.git_file>);
        Git git = new Git(userRepo);
        BranchTrackingStatus bts = BranchTrackingStatus.of(git.getRepository(),
        int aheadCount = bts.getAheadCount();
        int behindCount = bts.getBehindCount();
        if (aheadCount == 0 && behindCount == 0) {
            return TrackingStatus.SAME;
        } else if (aheadCount > 0 && behindCount == 0) {
            return TrackingStatus.AHEAD;
        } else if (aheadCount == 0 && behindCount > 0) {
            return TrackingStatus.BEHIND;
        } else {
            return TrackingStatus.DIVERGED;

    jgit has this:

    RevCommit org.eclipse.jgit.merge.Merger.getBaseCommit(RevCommit a, RevCommit b) throws IncorrectObjectTypeException, IOException

    take a try

    Git Baby is a git and github fan, let's start git clone.