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

前端 未结 24 2399
你的背包
你的背包 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:18

    The following works for me in case you need last two tags (for example, in order to generate change log between current tag and the previous tag). I've tested it only in situation where the latest tag was the HEAD.

    PreviousAndCurrentGitTag=`git describe --tags \`git rev-list --tags --abbrev=0 --max-count=2\` --abbrev=0`
    PreviousGitTag=`echo $PreviousAndCurrentGitTag | cut -f 2 -d ' '`
    CurrentGitTag=`echo $PreviousAndCurrentGitTag | cut -f 1 -d ' '`
    
    GitLog=`git log ${PreviousGitTag}..${CurrentGitTag} --pretty=oneline | sed "s_.\{41\}\(.*\)_; \1_"`
    

    It suits my needs, but as I'm no git wizard, I'm sure it could be further improved. I also suspect it will break in case the commit history moves forward. I'm just sharing in case it helps someone.

提交回复
热议问题