How to get the latest tag name in current branch in Git?

前端 未结 24 2183
你的背包
你的背包 2020-11-22 17:04

What\'s the simplest way to get the most recent tag in Git?

git tag a HEAD
git tag b HEAD^^
git tag c HEAD^
git tag

output:



        
24条回答
  •  名媛妹妹
    2020-11-22 17:25

    git log --tags --no-walk --pretty="format:%d" | sed 2q | sed 's/[()]//g' | sed s/,[^,]*$// | sed  's ......  '
    

    IF YOU NEED MORE THAN ONE LAST TAG

    (git describe --tags sometimes gives wrong hashes, i dont know why, but for me --max-count 2 doesnt work)

    this is how you can get list with latest 2 tag names in reverse chronological order, works perfectly on git 1.8.4. For earlier versions of git(like 1.7.*), there is no "tag: " string in output - just delete last sed call

    If you want more than 2 latest tags - change this "sed 2q" to "sed 5q" or whatever you need

    Then you can easily parse every tag name to variable or so.

提交回复
热议问题