Count the number of commits on a Git branch

后端 未结 12 929
[愿得一人]
[愿得一人] 2020-12-02 04:28

I found this answer already: Number of commits on branch in git but that assumes that the branch was created from master.

How can I count the number of commits along

相关标签:
12条回答
  • 2020-12-02 04:54

    To count the commits for the branch you are on:

    git rev-list --count HEAD
    

    for a branch

    git rev-list --count <branch-name>
    

    If you want to count the commits on a branch that are made since you created the branch

    git rev-list --count HEAD ^<branch-name>
    

    This will count all commits ever made that are not on the branch-name as well.

    Examples

    git checkout master
    git checkout -b test
    <We do 3 commits>
    git rev-list --count HEAD ^master
    

    Result: 3

    If your branch comes of a branch called develop:

    git checkout develop
    git checkout -b test
    <We do 3 commits>
    git rev-list --count HEAD ^develop
    

    Result: 3

    Ignoring Merges

    If you merge another branch into the current branch without fast forward and you do the above, the merge is also counted. This is because for git a merge is a commit.

    If you don't want to count these commits add --no-merges:

    git rev-list --no-merges --count HEAD ^develop
    
    0 讨论(0)
  • 2020-12-02 04:54

    As the OP references Number of commits on branch in git I want to add that the given answers there also work with any other branch, at least since git version 2.17.1 (and seemingly more reliably than the answer by Peter van der Does):

    working correctly:

    git checkout current-development-branch
    git rev-list --no-merges --count master..
    62
    git checkout -b testbranch_2
    git rev-list --no-merges --count current-development-branch..
    0
    

    The last command gives zero commits as expected since I just created the branch. The command before gives me the real number of commits on my development-branch minus the merge-commit(s)

    not working correctly:

    git checkout current-development-branch
    git rev-list --no-merges --count HEAD
    361
    git checkout -b testbranch_1
    git rev-list --no-merges --count HEAD
    361
    

    In both cases I get the number of all commits in the development branch and master from which the branches (indirectly) descend.

    0 讨论(0)
  • 2020-12-02 04:55

    How much commits was done to current branch since begin of history, not counting commits from merged branches:

    git rev-list HEAD --count --first-parent
    

    From documentation git rev-list --help:

    --first-parent

    Follow only the first parent commit upon seeing a merge commit. This option can give a better overview when viewing the evolution of a particular topic branch, because merges into a topic branch tend to be only about adjusting to updated upstream from time to time, and this option allows you to ignore the individual commits brought in to your history by such a merge. Cannot be combined with --bisect.

    Note: Shallow clone will shrink the history size. E.g. if you clone with --depth 1, will return 1.

    number of commits done since some other commit:

    git rev-list HEAD abc0923f --count --first-parent
    

    or the same:

    git rev-list abc0923f.. --count --first-parent
    

    or use any other git reference:

    git rev-list master tag-v20 --count --first-parent
    

    Count commits done since 2018 year

    git rev-list HEAD --count --first-parent --since=2018-01-01
    

    01-01-2018, 01.01.2018, 2018.01.01 also works.


    git rev-label

    I wrote a script to get version-revision from Git in format like '$refname-c$count-g$short$_dirty' which expands to master-c137-gabd32ef.
    Help is included to script itself.

    0 讨论(0)
  • 2020-12-02 04:56

    To see total no of commits you can do as Peter suggested above

    git rev-list --count HEAD
    

    And if you want to see number of commits made by each person try this line

    git shortlog -s -n
    

    will generate output like this

    135  Tom Preston-Werner
    15  Jack Danger Canty
    10  Chris Van Pelt
    7  Mark Reid
    6  remi
    
    0 讨论(0)
  • 2020-12-02 05:00

    One way to do it is list the log for your branch and count the lines.

    git log <branch_name> --oneline | wc -l
    
    0 讨论(0)
  • 2020-12-02 05:01

    You can use this command which uses awk on git bash/unix to get the number of commits.

        git shortlog -s -n | awk '/Author/ { print $1 }'
    
    0 讨论(0)
提交回复
热议问题