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

后端 未结 6 580
北恋
北恋 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 13:59

    A better solution (from Greg Hewgill's answer in the other question) would be:

    git name-rev --name-only --tags HEAD
    

    If it returns "undefined" then you are not on a tag. Otherwise it returns the tag name. So a one-liner to do something like my other answer would be:

    git_tag=`git name-rev --name-only --tags HEAD | sed 's/^undefined$//'`
    

    Interactive shell example of how it works:

    $ git checkout master
    Already on "master"
    $ git name-rev --name-only --tags HEAD
    undefined
    $ git checkout some-tag
    Note: moving to "some-tag" which isnt a local branch
    If you want to create a new branch from this checkout, you may do so
    (now or later) by using -b with the checkout command again. Example:
      git checkout -b 
    HEAD is now at 1234567... Some comment blah blah
    $ git name-rev --name-only --tags HEAD
    some-tag
    

提交回复
热议问题