47

Using the git diff --shortstat my_branch master is a great way to tell how many files changes and the insertions and deletions. I read the git diff documentation, but I couldn't find a way to tell the number of commits between my_branch and master. Does that exist?

sobi3ch
  • 2,555
  • 2
  • 31
  • 41
nickcoxdotme
  • 6,567
  • 9
  • 46
  • 72

5 Answers5

43

I think you could try:

git log --oneline mybranch ^master

For the exact count:

git log --oneline mybranch ^master | wc -l

Should give you the information you want.

Daniel Williams
  • 8,673
  • 4
  • 36
  • 47
38

I found this to be much simpler:

git rev-list --count my-branch ^master
etech
  • 2,548
  • 1
  • 27
  • 24
  • missing .. between branches as of git version 2.13.5 (Apple Git-94) – Intel Nov 01 '17 at 07:03
  • 1
    `..` not needed with git version 2.14.2 on Ubuntu Linux. – bovender Mar 28 '18 at 15:37
  • 3
    If you're already on `my-branch` you can use `HEAD` instead: `git rev-list --count HEAD ^master` – David Jan 06 '21 at 09:20
  • `my-branch ^master` is the same as `^master my-branch` and `master..my-branch`. This notation lists all commits which are in `my-branch` but are not yet in `master`. You can also use `master...my-branch` (which is the same as `my-branch...master` or just `...master` if you're currently on `my-branch`) which lists commits unique to `my-branch` and `master`, i.e. how much the two branches diverged. – SnakE May 24 '22 at 13:21
25

I could not get etech's answer to work, but this works for me on OS X:

git rev-list --count master..my-branch

my-branch can be omitted for changes on the current branch.

mvr
  • 1,732
  • 16
  • 11
4

Another option. The hashes/number are just examples

$ git checkout my_branch
$ git cherry master

+ 950b187c4b28844680df7008cfa3b348c1a46016
+ 109e427cbe84c3c8fc0ac2fbfb5120bcc2511933
+ 9bc217c0bcabbd6aa4ba88bbaaad23805994f90d
+ 74e054614a1bb1c442fbcf53926dcb910097321c
+ b34ae1ce8a58e0cbdbe1657ebca81a3036d7c72d

Will give a list of commit hashes that are in my_branch but not in master

$ git checkout my_branch
$ git cherry master | wc -l
5

Will give the number of commits that are in my_branch but not in master

Read more about git cherry here

Community
  • 1
  • 1
Tim
  • 41,901
  • 18
  • 127
  • 145
0

This gives me the number of commits I've made to branch_being_merged_in since branching from branch_A:

git log --pretty=%H branch_A..branch_being_merged_in | wc -l