1

We are working on a versioning system based on git tags. While git describe is great, we would prefer commands that output the three elements of git describe seperately (latest tag, commits since tag, commitId) e.g. TAG 12 gff9fd30. We managed getting the commitId with git rev-parse HEAD, but don't know if there are any commands for only getting the latest tag and the commits since that tag.

Of course it would be possible to split the output at hyphens, but if there is a cleaner solution, we would prefer it.

Thanks for any hints!

2 Answers2

1

only getting the latest tag

git describe --tags --abbrev=0

commits since that tag

git log LATEST_TAG..HEAD, you can use --pretty to format the logs.

fannheyward
  • 18,599
  • 12
  • 71
  • 109
1

Latest tag in the current branch:

git describe --tags --abbrev=0

(Found in https://stackoverflow.com/a/7261049/7976758, search: https://stackoverflow.com/search?q=%5Bgit%5D+latest+tag)

Count commits since a commit:

git rev-list --count <revision>..

(Please note ..; https://stackoverflow.com/a/4061706/7976758, https://stackoverflow.com/search?q=%5Bgit%5D+count+commits).

Overall:

git rev-list --count $(git describe --tags --abbrev=0)..

Or

lasttag=$(git describe --tags --abbrev=0)
git rev-list --count $lasttag..
phd
  • 82,685
  • 13
  • 120
  • 165
  • I don't know if I'm doing something wrong, but `git rev-list --count $(git describe --tags --abbrev=0)..` gives me the total count of commits on the branch, not just since the latest tag. – Meister der Magie Jan 29 '23 at 11:41
  • 1
    @MeisterderMagie Works for me. No spaces before `..`. Try quotes: `git rev-list --count "$(git describe --tags --abbrev=0).."` or even `git rev-list --count "$(git describe --tags --abbrev=0)..HEAD"` (because `..` actually means `..HEAD`; no spaces). – phd Jan 29 '23 at 11:53
  • The quotes did the job. Thank you! What is the difference between using quotes or not? – Meister der Magie Jan 29 '23 at 11:57
  • 1
    @MeisterderMagie I suspect extra shell metacharacters. But hard to say without thorough debugging. – phd Jan 29 '23 at 11:58