Home > Mobile >  Get number of commits since a tag from another branch with a git command
Get number of commits since a tag from another branch with a git command

Time:10-26

I'm trying to find the number of commits since a tag, in another branch than main (main beeing the branch where the tags are created).
I've checked several q/a around here suggesting 'git rev-list' and 'git describe' (with lots of combinations of flags and different order of parameters) without much success, in my case to get the right number...

For our example, we have 2 branches being main and dev.

In our workflow we tag main with the new version on release. We then merge main to dev. Starting from there I would love to get the number of commits in dev, since this version/tag only.

Let's say we've just tagged main with 'v1.2.3' and merged main into dev. If I change a file in main and commit it, the following command will yield '1' and that's perfect.

`git rev-list v1.2.3..HEAD --count`  

Now if I checkout dev and use the same command in this context, I'm getting something like 17. I want to obtain 0 in this case (or 1 for the merge entry).
If I then edit/commit a file here in dev and run this command I've got 18; the 1 is expected.

How to "reset" this counter to 0 when I merge main into dev and start counting from then? Or what's the good parameters to use?
Again I tried with the parameters --first-parent / --no-merges and so on; I can reduce the number but never get 0 or 1 (1 might be for the commit of the merge, and I'll be fine with that as well)

Thanks

CodePudding user response:

Those 17 commits are most likely siblings to the v1.2.3 commit, accumulated in dev since main was frozen for release testing. They neither precede nor follow v1.2.3 in the history. In this case, it looks like git is telling you about all the commits since the common ancestor of the two branches.

If you count from the merge commit instead of the tag, you'll get the zero you're looking for, but that discounts those 17 commits to dev and makes it appear that your current state in dev is v1.2.3 when it's really v1.2.3 17 other commits.

Sample repo:

* eb8a717 | HEAD -> dev | first commit to dev after main merged back in
*   2862ad6 |  | Merge branch 'main' into dev
|\  
| * 33cf7dd | tag: v1.2.3, main| patch to initial version
* | 9458df5 |  | second commit to dev since branching
* | 53d6d29 |  | first commit to dev since branching
|/  
* 2ffc809 |  | first commit


$ git rev-list --count v1.2.3...HEAD
4

$ git rev-list --count 2862ad6...HEAD
1

Perhaps you want to tag the merge commit as v1.2.4 or v1.3.0 or v2.0.0 as appropriate, and count from that tag instead?

  •  Tags:  
  • git
  • Related