How to programmatically determine whether the Git checkout is a tag and if so, what is the tag name

后端 未结 6 578
北恋
北恋 2020-12-23 13:14

In a Unix or GNU scripting environment (e.g. a Linux distro, Cygwin, OSX), what is the best way to determine whether the current checkout is a Git tag. If it is a tag, how c

6条回答
  •  别那么骄傲
    2020-12-23 14:18

    You cannot determine if the current checkout “is a tag”. You can only determine if the current checkout was a commit that has tags.

    The difference is: if there are several tags pointing to this commit, git can’t tell you which you used to checkout, nor if you actually used one to get there at all.

    Jakub’s answer here based on git describe --exact-match (--tags) gives you “the first” of all the (annotated) tags.

    And git describe sorts them like this:

    • annotated tags first
      • sorted youngest first
    • lightweight tags come afterwards
      • binary sorted by tag name (that means alphabetically if it’s English encoded in ASCII)
      • git doesn’t store meta-data with lightweight tags, so “youngest-first” can not be realized

提交回复
热议问题