Search code examples
gitbitbucketbranchatlassian-sourcetreebranching-and-merging

Calculate how different a git branch is from master?


I want to inform / warn developers on a project if they are working on a git branch that has diverged a lot from the current master branch.

What's the simplest way to calculate how different the tip of a branch is from the tip of the master branch?

I was considering checking the number of commits on both branches since they last diverged, and informing the developer if that number is greater than some threshold such as 10 commits. Would this command work for this purpose, if run as a commit hook?

git log --oneline master...test/countcommitdifference | wc -l
>      3

(My team uses git, SourceTree, and BitBucket.)


Solution

  • That would work, but for scripting it is advisable to use git rev-list instead:

    git rev-list master...your_branch | wc -l
    

    or as @jthill mentioned in the comments, simply git rev-list --count master...your_branch.

    (This will give you the number of commits on master plus number of commits on your_branch).

    Another option might be to use git diff --shortstat, which shows you the number of changed files, inserted and deleted lines. The first number is always the number of changed files:

    git diff --short-stat master your_branch | awk '{print $1}'