Here using JGit using BranchTrackingStatus:
public enum TrackingStatus { SAME, BEHIND, AHEAD, DIVERGED } public TrackingStatus getTrackingStatus() throws IOException, GitAPIException { Repository userRepo = new FileRepository(<path_to_.git_file>); Git git = new Git(userRepo); git.fetch().call(); BranchTrackingStatus bts = BranchTrackingStatus.of(git.getRepository(), git.getRepository().getBranch()); 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; } }
source share